Zend Framework:我可以在模型中设置视图变量吗?
在我的控制器中,我通常这样做:(
$this->view->foo = "bar";
我将此变量称为 VIEW-VARS)
在视图脚本中,我将其渲染为:
echo $this->foo;
所以,我想知道是否可以在模型内(而不是在控制器中)定义“视图变量”在视图脚本中呈现。
In my controller, I usually do:
$this->view->foo = "bar";
(I call this vars, as VIEW-VARS)
In view script, I render this with:
echo $this->foo;
So, I wonder if it's possible to define "view vars" inside models(not in controllers) that can be rendered in the view scripts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将信息分配给视图是控制器的工作,并且执行您所建议的操作会混淆 MVC 模式的边界。
您真正应该做的是将模型分配给视图(在控制器中),然后在视图中访问模型的各个部分。或者,您可以仅在控制器中分配模型的相关部分。
Assigning information to the view is the job of the controller, and doing what you're suggesting would muddy the boundaries of the MVC pattern.
What you should really be doing is assigning the model to the view (in the controller), and then access the various parts of the model within the view. Alternatively, you can assign only the relevant parts of the model in your controller.
忽略模型确实不应该负责配置视图这一事实,在某些情况下它是允许的(例如 ViewModel 模式)。
有几种检索或提供视图实例的方法:
$model->setView($this->view)
)。ViewRenderer
帮助器检索视图实例:$view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
Ignoring the fact that a model should indeed not be responsible for configuring the view, there are cases in which it is allowed (for instance the ViewModel pattern).
There are a couple of ways of retrieving or providing a view instance:
$model->setView($this->view)
).ViewRenderer
helper:$view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->bootstrap('view')->getResource('view');