哪种方法可以更好地获取 Zend_View

发布于 2024-12-05 18:22:13 字数 392 浏览 0 评论 0原文

我正在开发基于 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 技术交流群。

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

发布评论

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

评论(4

Saygoodbye 2024-12-12 18:22:13

由于 Zend_Layout::getView() 方法如下所示:

public function getView()
{
    if (null === $this->_view) {
        require_once 'Zend/Controller/Action/HelperBroker.php';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        if (null === $viewRenderer->view) {
            $viewRenderer->initView();
        }
        $this->setView($viewRenderer->view);
    }
    return $this->_view;
}

...我更愿意使用它;)

Since Zend_Layout::getView() method looks like the following:

public function getView()
{
    if (null === $this->_view) {
        require_once 'Zend/Controller/Action/HelperBroker.php';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        if (null === $viewRenderer->view) {
            $viewRenderer->initView();
        }
        $this->setView($viewRenderer->view);
    }
    return $this->_view;
}

... i would prefer to use it ;)

沐歌 2024-12-12 18:22:13

最好从 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.

自此以后,行同陌路 2024-12-12 18:22:13

Zend_Controller_Action_Helper 定义了 getActionController(),您可以使用它获取公共视图。也许插件有相同的方法。虽然我不喜欢通过公共财产访问它,但我相信通过助手的控制器本身比布局更好。

$view = $this->getActionController()->view;

Zend_Controller_Action_Helper defines getActionController() 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.

$view = $this->getActionController()->view;
霊感 2024-12-12 18:22:13

如果您从布局中提取视图,则两者是两个不同的事情,而不是返回视图实例基本上将帮助您访问 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) .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文