Disable layout and view renderer in Zend Framework

Posted on in Development

Just a quick tip: I’m working on a project using Zend Framework and needed to switch off layout and view renderer for a specific controller. This can be achieved by adding the following calls to the controller’s preDispatch() method:

public function preDispatch()
{
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);
}
  • http://twitter.com/parsifal SeanG

    Great! Thanks! It’s wonderful to actually have this documented somewhere now!

  • http://twitter.com/parsifal SeanG

    Great! Thanks! It’s wonderful to actually have this documented somewhere now!

  • Dmitry

    View is a helper so, you can access it similar way as layout:
    $this->_helper->viewRenderer->setNoRender(true);

    • http://maff.ailoo.net/ Mathias

      Thanks for the addition Dmitry :)

  • Dmitry

    View is a helper so, you can access it similar way as layout:
    $this->_helper->viewRenderer->setNoRender(true);

    • http://maff.ailoo.net/ Mathias

      Thanks for the addition Dmitry :)

  • Marco

    Thanks man. I needed to do this to use ZF with dojo ajax methods.

    (if there’s another/better way, please fwd that to me)

  • Marco

    Thanks man. I needed to do this to use ZF with dojo ajax methods.

    (if there’s another/better way, please fwd that to me)

  • Sebastien

    Pay attention to put away the last “?>” in your controller !!

  • Sebastien

    Pay attention to put away the last “?>” in your controller !!

  • http://unlikelyteacher.com Paul

    Thanks! I needed this for rico ajax.

  • http://unlikelyteacher.com Paul

    Thanks! I needed this for rico ajax.

  • Frank

    For JSON answers, put this in the init() function of your controller:

    [source lang=""]$ajaxContext = $this->_helper->getHelper(‘AjaxContext’);
    $ajaxContext->addActionContext(‘actionName’, ‘json’)
    ->initContext();[/source]

    Only thing you have to do is an addition to the url you are calling within your AJAX function:

    /controllerName/actionName/format/json

    Every variable you give to the view with

    $this->view->variableName

    will be automatically JSON-encoded.

    You can also call XML or HTML answers: simply change ‘json’ with ‘xml’ or ‘html’ in both the addActionContext function and the url format parameter in your javascript.

    Then, you will need a view script with the following name rules:
    XML: ‘action-name.xml.phtml’
    HTML: ‘action-name.ajax.phtml’

    XML not testet since I prefer JSON…

    • http://maff.ailoo.net/ Mathias

      Nice one, thanks for the addition :)

  • Frank

    For JSON answers, put this in the init() function of your controller:

    [source lang=""]$ajaxContext = $this->_helper->getHelper(‘AjaxContext’);
    $ajaxContext->addActionContext(‘actionName’, ‘json’)
    ->initContext();[/source]

    Only thing you have to do is an addition to the url you are calling within your AJAX function:

    /controllerName/actionName/format/json

    Every variable you give to the view with

    $this->view->variableName

    will be automatically JSON-encoded.

    You can also call XML or HTML answers: simply change ‘json’ with ‘xml’ or ‘html’ in both the addActionContext function and the url format parameter in your javascript.

    Then, you will need a view script with the following name rules:
    XML: ‘action-name.xml.phtml’
    HTML: ‘action-name.ajax.phtml’

    XML not testet since I prefer JSON…

    • http://maff.ailoo.net/ Mathias

      Nice one, thanks for the addition :)

  • http://www.facebook.com/people/Tobias-Strebitzer/1419257988 Tobias Strebitzer

    nice!