Zend Framework:我可以在模型中设置视图变量吗?

发布于 2024-12-25 14:58:45 字数 228 浏览 3 评论 0原文

在我的控制器中,我通常这样做:(

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2025-01-01 14:58:45

将信息分配给视图是控制器的工作,并且执行您所建议的操作会混淆 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.

月亮邮递员 2025-01-01 14:58:45

忽略模型确实不应该负责配置视图这一事实,在某些情况下它是允许的(例如 ViewModel 模式)。
有几种检索或提供视图实例的方法:

  • 注入视图实例。从控制器中,您可以通过注入实例来为模型提供实例($model->setView($this->view))。
  • 通过 ViewRenderer 帮助器检索视图实例: $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
  • 通过应用程序引导程序检索视图实例: <代码>$视图= Zend_Controller_Front::getInstance()->getParam('bootstrap')->bootstrap('view')->getResource('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:

  • Inject the view instance. From the controller you can provide the model the instance by injecting it ($model->setView($this->view)).
  • Retrieve the view instance via the ViewRenderer helper: $view = Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->view;
  • Retrieve the view instance via the Application bootstrapper: $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->bootstrap('view')->getResource('view');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文