哪种方法可以更好地获取 Zend_View
我正在开发基于 Zend Framework 的应用程序,在控制器插件中,我可以使用以下方法获取 Zend_View 对象,请有人告诉我哪种方法更好,为什么?
$view = Zend_Layout::getMvcInstance()->getView();
或者
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view)
$viewRenderer->initView();
$view = $viewRenderer->view;
I am working on a Zend Framework based app, and in controller plugins, I can get Zend_View object with the following methods, someone please tell me which approach is better and why?
$view = Zend_Layout::getMvcInstance()->getView();
or
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
if (null === $viewRenderer->view)
$viewRenderer->initView();
$view = $viewRenderer->view;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
Zend_Layout::getView()
方法如下所示:...我更愿意使用它;)
Since
Zend_Layout::getView()
method looks like the following:... i would prefer to use it ;)
最好从 viewRenderer 中获取它,因为这样你就可以确定你总是会得到它。在某些情况下您可能没有使用布局,然后您将无法通过布局获取视图。
因此,为了节省开支,请从 viewRenderer 中提取它,无论如何它更直接,因此也更快。
It is better to pull it from the viewRenderer because then you're sure you'll always get it. You may not be using Layout in some contexts and then you won't get the view through the layout.
So, to be on the save side, pull it from the viewRenderer, it's more direct anyways and therefore faster too.
Zend_Controller_Action_Helper
定义了getActionController()
,您可以使用它获取公共视图。也许插件有相同的方法。虽然我不喜欢通过公共财产访问它,但我相信通过助手的控制器本身比布局更好。Zend_Controller_Action_Helper
definesgetActionController()
with which you can acquire the public view. Perhaps plugins have the same method. While I'm not fond of accessing it through a public property, I believe it's preferable to go through the helper's controller itself than the layout.如果您从布局中提取视图,则两者是两个不同的事情,而不是返回视图实例基本上将帮助您访问 layout.phtml 内的视图变量,另一方面,从 viewRender 操作助手获取视图将帮助您访问操作中的视图变量.phtml(特定控制器操作的视图)。
both are two different things if you are pulling the view from layout than the return view instance basically will help you access view variables inside your layout.phtml , on the other hand taking view from viewRender action helper will help you access view variables in your action.phtml (view for specific controller action) .