Chris Matta
Chris Matta

Reputation: 3433

Can Zend Framework 1.x disable layouts and rendering on an entire module?

I have a module called api that I would like to flatly disable all rendering and layout and only return JSON. I know I can disable layouts per action in a controller like so:

$this->_helper->_layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);

But how can I do it on the entire api module?


Solution: Put this in it's own controller and have all the other controllers extend this:

public function preDispatch() {
        $this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
    }

Upvotes: 1

Views: 705

Answers (3)

Volvox
Volvox

Reputation: 1649

Create plugin and add preDispatch() method like this:

public function preDispatch(Zend_Controller_Request_Abstract $request)
    if ($request->getModuleName() === 'messages') {
        Zend_Layout::getMvcInstance()->disableLayout();
        Zend_Controller_Action_HelperBroker::getExistingHelper('viewRenderer')->setNeverRender(true);
    }

From now on module 'messages' will have Layout and view disabled.

Upvotes: 1

b.b3rn4rd
b.b3rn4rd

Reputation: 8830

class My_Controller_Action extends Zend_Controller_Action
{
    public function init()
    {
        $this->_helper->_layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(TRUE);
    }
}
class Api_IndexController extends My_Controller_Action
{
     public function viewAction()
     {
        // data to return
        $data = array();
        $this->_helper->json($data);
     }
}

Upvotes: 2

qdelettre
qdelettre

Reputation: 1873

Maybe you can create a parent controller with your code, that all your module controller will extend.

Upvotes: 0

Related Questions