Set a generic layout for all modules in Zend frame

2019-02-11 07:23发布

问题:

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

回答1:

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);
}