Gnuffo1
Gnuffo1

Reputation: 3546

Cannot call setNoRender() on viewRenderer in postDispatch() in a Zend Framework 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 echoes in here and they will output.

Upvotes: 4

Views: 4077

Answers (4)

Tahir
Tahir

Reputation: 733

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

Upvotes: 1

humbads
humbads

Reputation: 3412

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.

Upvotes: 0

user228395
user228395

Reputation: 1176

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

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

Upvotes: 5

Adam Pointer
Adam Pointer

Reputation: 1492

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

Upvotes: 1

Related Questions