访问模型/控制器中的视图

访问模型/控制器中的视图

问题描述:

我有一个像这样的 MyData.php 类:

class myData {
  function render() {
    $view = new Zend_View();
    $view->str = 'This is string.';
    echo $view->render('myview.phtml');
  }
}

和一个 myview.phtml 文件:

<div id='someid'><?= $this->str ?></div>

在另一个视图中,我正在做这样的事情:

In another view I am doing something like this:

<?php
    $obj = new myData ();
    $obj->render(); // it should be <div id='someid'>This is string.</div>
?>

它给了我以下异常:

Message: no view script directory set; unable to determine location for view script

MyData.phpmyview.phtml 在同一目录中.

MyData.php and myview.phtml are in same directory.

我是这样做的:

我将我的 myview.phtml 更改为 myview.php

<div id='someid'><?= $this->str ?></div>

在 myData 类渲染函数中:

In myData class render function:

class myData {
  function render() {
    $view = new Zend_View();
    $view->setScriptPath( "/Directory/Path/For/myview/php/file" );
    $view->str = 'This is string.';
    echo $view->render('myview.php');
  }
}

一切都如我所问的那样工作.我的代码中缺少 $view->setScriptPath($path);.

And all things are working as I asked in question. I was missing $view->setScriptPath($path); in my code.

帮助: