1

I'm working on a ZF2 Project, and I have some modules in my directories :

/module/module1
/module/module2
/module/module3
/module/module4
[...]

But, in each module I also have a specific layout, respectively :

/module/module1/view/layout/layout.phtml
/module/module2/view/layout/layout.phtml
/module/module3/view/layout/layout.phtml
/module/module4/view/layout/layout.phtml

My question is : How can I set a generic layout for all my modules without to have to modify each layout when I want.

Thank you

Léo
  • 792
  • 1
  • 7
  • 27

1 Answers1

9

You can set the layout to be what ever you want in each modules config, just change the layout to be what ever you want:

module.config.php or inside getConfig()

'view_manager' => array(
    // other stuff here.. 
    'template_map' => array(
        // use Applications layout instead
        'layout/layout' => __DIR__ . '/../Application/view/application/layout/layout.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

Or you can set each module to selectively set it's layout in Module.php:

Module.php

/**
 * Initialize
 */
public function init(ModuleManager $manager)
{
    $events = $manager->getEventManager();
    $sharedEvents = $events->getSharedManager();
    $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
        /* @var $e \Zend\Mvc\MvcEvent */
        // fired when an ActionController under the namespace is dispatched.
        $controller = $e->getTarget();
        $routeMatch = $e->getRouteMatch();
        /* @var $routeMatch \Zend\Mvc\Router\RouteMatch */
        $routeName = $routeMatch->getMatchedRouteName();
        $controller->layout('application/layout/layout');
    }, 100);
}
Andrew
  • 12,617
  • 1
  • 34
  • 48
  • This is a great help... what if I have only to implement an different navigation and css for each module? Now I implement the navigation with partials... – cwhisperer Oct 21 '13 at 10:26
  • Dear Expert, Your second solution is not firing if i have submodule with different name because namespace is different. If any module have multiple sub modules. How can we set layout for all submodule. Thanks in advance – karim_fci Jun 23 '14 at 09:52