Change layout in the controller of Zend Framework 2.0

Alex Pliutau picture Alex Pliutau · Feb 2, 2012 · Viewed 32.4k times · Source

I am learning ZF2.0 beta. In ZF1 we can change layout on the fly in controller:

Zend_Layout::getMvcInstance()->setLayout('layoutname');

But in ZF2.0 Zend\Layout class doesn't has method getMvcInstance(). I think that it can be made using dependency injections but not sure.

Answer

Jurian Sluiman picture Jurian Sluiman · Feb 23, 2012

The ZF2 is heavily under development and no guarantee can be made the way it works now, will be the way it works when ZF2 reaches a stable state.

However, the new view layer from Zend\Mvc is recently merged so you should be able to do this now (with current master):

public function somethingAction () 
{
    // Do some intelligent work

    $this->layout('layout/different');
}