开发者

Cannot call setNoRender() on viewRenderer in postDispatch() in a Zend Framework Controller Plugin

开发者 https://www.devze.com 2023-02-19 05:43 出处:网络
Calling setNoRender() or indeed any methods on the viewRenderer helper seem to have no effect in a controller plugin.

Calling setNoRender() or indeed any methods on the viewRenderer helper seem to have no effect in a controller plugin.

class TestPlugin extends Zend_Controller_Plugin_Abstract
{
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setNoRender();
    }
}

The view script still renders. And the plugin is definitely running as I can put开发者_如何学C echoes in here and they will output.


You'll have to put this in your postDispatch of your Controller Plugin.

$viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
$viewRenderer->setNeverRender(true);


Does this work in any other hooks, for example preDispatch()?


in case if someone wants to disable both layout and view using controller plugin, here is the preDispatch hook that i got working with the help of different articles and answers, including this one. Hope it helps someone, and saves some time.

// in Controller Plugin
public function preDispatch(){
        //if  its an AJAX request then disable layout and view.
        if ($this->_request->isXmlHttpRequest() || isset($_GET['ajax'])){
            // disable layout
            $layout = Zend_Controller_Action_HelperBroker::getExistingHelper('Layout');
            $layout->disableLayout();
            // disable view
            $viewRenderer = Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer');
            $viewRenderer->setNeverRender(true);
        }
    }


I also could not figure out how to do this from the controller plugin initialization script. However, there is a simple workaround. You can do this in the preDispatch of your base controller with the following, standard code:

$this->_helper->viewRenderer->setNoRender(true);

All your controllers should be inheriting from this base controller, which itself extends Zend_Controller_Action.

0

精彩评论

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