访问模型/控制器中的视图
我有一个像这样的 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>
在另一个视图中,我正在做这样的事情:
<?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.php 和 myview.phtml 位于同一目录中。
I have a class MyData.php like this:
class myData {
function render() {
$view = new Zend_View();
$view->str = 'This is string.';
echo $view->render('myview.phtml');
}
}
and a myview.phtml file:
<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>
?>
It is giving me following exception:
Message: no view script directory set; unable to determine location for view script
MyData.php and myview.phtml are in same directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在创建一个新的 Zend_View 实例。你不应该这样做。要获取现有的视图实例,您可以执行以下操作:
另外,我认为视图脚本路径应该相对于 APPLICATION_PATH/views/scripts 文件夹。
You are creating a new Zend_View instance. You should not do this. To get the existing view instance you could do as follows:
Also, I think that the view script path should be relative to
APPLICATION_PATH/views/scripts
folder.我是这样做的:
我将 myview.phtml 更改为 myview.php
在 myData 类渲染函数中:
并且所有事情都按照我所问的方式工作。我的代码中缺少
$view->setScriptPath($path);
。帮助:
I did it like this:
I changed my myview.phtml to myview.php
<div id='someid'><?= $this->str ?></div>
In myData class render function:
And all things are working as I asked in question. I was missing
$view->setScriptPath($path);
in my code.Help:
如果您使用完整的 MVC 堆栈,那么您最好只为此类事物创建一个视图助手...或者简单地传递使用部分视图助手并将您的对象传递给它。
例如,
在控制器中使用现有的 Zend_View_Helper_Partial... 创建 myData 对象并将其分配给视图:
在操作的视图中:
然后在
myview.phtml
中您可以执行以下操作 :例如,看起来您甚至根本不需要 myData 对象,您只需将 str 变量分配给视图并将其传递给局部视图,而不是创建对象。
您应该阅读
Zend_View
文档。 。If you are usin the full MVC stack its youre better off just creating a view helper for this type of thing... or simply passing the using the Partial view helper and passing your object to it.
For example with the exisiting Zend_View_Helper_Partial....
in your controller create the myData object and assign it to the view:
in the view for the action:
Then in your
myview.phtml
you can do:For your example it looks like you dont even need the myData object at all you can just assign the
str
variable to the view and pass it along to the partial instead of creating an object.You should read the
Zend_View
docs...