3

How to make my ZF2 module load other layout file for specific controller ?

Consider you have IndexController and AdminController in your ZF2 application module and the IndexController is using layout.phtml but you want to use adminlayout.phtml for AdminController.

How is it possible?

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
Sina Miandashti
  • 2,087
  • 1
  • 26
  • 40

3 Answers3

7
class Module {
    public function onBootstrap($e) {
        $em  = $application->getEventManager();

        $em->attach(MvcEvent::EVENT_DISPATCH, function($e) {
            $controller = $e->getTarget();
            if ($controller instanceof Controller\AdminController) {   
                $controller->layout('layout/layoutadmin.phtml');
            } else {
                $controller->layout('layout/layout.phtml');
            }   
        });
    }
}

and don't forget to register your new controller by adding this config in your module config file:

'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Admin' => 'Application\Controller\AdminController',
    ),
),
superdweebie
  • 1,576
  • 14
  • 23
Sina Miandashti
  • 2,087
  • 1
  • 26
  • 40
  • 2
    Just a thought for others who might read this. If you think you need different layouts, there may be something that could be improved in your archetecture. Generally you should only need one layout. If you need two layouts, maybe you should actually have two separate apps (or two separate entry points). Or, maybe you need to use the new ZF2 nested view model system more thoughtfully...just thoughts. – superdweebie Aug 12 '12 at 22:22
  • maybe its good to use A module for index then a module for Admin – Sina Miandashti Aug 14 '12 at 09:40
  • I've fixed your markup. Providing an explanation and pros/cons with this solution would improve this answer a lot. (For instance, why or why not do this instead of manually specify the layout in the controller?) – Matsemann Aug 14 '12 at 11:50
  • @superdweebie I disagree. App might have both frontend and admin that might look totally different and might require different layouts. Or how about authentication pages -> that also might require different layout. So it's completely plausible that 2 controllers need different layouts. – Dannyboy Feb 05 '14 at 16:44
  • `$application` is an undefined variable. Where does it come from? Do you mean to use `$e->getApplication()` instead? – Leo Galleguillos Dec 19 '16 at 09:23
1

other best Solution :

'view_manager' => array(
        'template_path_stack' => array(
            'YOURMODULENAME' => __DIR__ . '/../view',
        ),

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

change YOURMODULENAME to your module specific name

Sina Miandashti
  • 2,087
  • 1
  • 26
  • 40
0

Rather than adding logic to your entire module in the onBootstrap method, you can use your controller's onDispatch method to simply set the layout for that entire controller.

In your admin controller, add a shortcut at the top:

use Zend\Mvc\MvcEvent;

Then, use the following onDispatch in your controller:

public function onDispatch(MvcEvent $mvcEvent)
{   
    $this->layout()->setTemplate('layout/admin');

    return parent::onDispatch($mvcEvent);
}

I believe this solution is better because the code only gets called when the admin controller is called.

Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43