开发者

Which is better approach to get Zend_View

I am working on a Zend Framework based app, and in controller plugins, I can get Zend_View object with the following开发者_StackOverflow 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;


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 ;)


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 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;


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) .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜