1

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

3 Answers 3

2
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);
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this inspired my solution.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.