Changing Layout in ZF2 Controller

Changing the layout or template within a Zend Framework 2 is easy. Thanks to the layout controller plugin, one can change the layout for a given controller action in just one line.

public function indexAction()
	return new ViewModel();

As you can see above, the only thing you have to do in order to change the layout for a ZF2 controller action, is to pass in the name of the layout to the layout controller plugin. Afterwards, simply return a view model or array as usual.

Make sure that the name that you passed to the layout controller plugin is available within the view manager’s template map. Simply add it to one of your module’s module.config.php (by convention) so it looks like below.

'view_manager' => array(
	'template_map' => array(
		'layout/default' => __DIR__ . '/../view/layout/default.phtml',

Simply adjust to your particular needs.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *