Disabling the layout in Zend Framework 2

 Posted by on August 16, 2012  General  Add comments  Tagged with: ,
Aug 162012
 

Sometimes you need to disable the layout for a specific action. To do this, you simply set the view model that your action returns as “terminal”. This tells ZF2 not to wrap the returned view model with a layout.

<?php
 
namespace Application\Controller;
 
use Zend\Mvc\Controller\ActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends ActionController
{
    public function nolayoutAction()
    {
        // Turn off the layout, i.e. only render the view script.
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
}

More examples can be found in Rob Allen’s ZF2TestApp.

  6 Responses to “Disabling the layout in Zend Framework 2”

  1. Noch nicht so superfein die methode, aber bei ZF2 war es eine der sachen die ich häufiger gebraucht habe.

  2. Sorry for posting in German, comes by crossreading through different languages. Just wanted to say thanks for tutorial as this one of the things i commonly needed in ZF1

  3. I’m wondering how to disable the layout for an entire module from the Module class. Can’t seem to find a way to do so? I should probably sign up for a mailing list or something? :-S

  4. How do I disable the layout for error pages?

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" color="" escaped="">