简体   繁体   中英

Different layout for different modules

Something really weird is happening. I have two modules, one called Application and the other one called Dashboard they are different and have nothing to do with each other. I wanted to use a phtml layout to each one of them, and that is what I did:

module/Application/config/module.config.php :

// ...

'view_manager' => [
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => [
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

module/Dashboard/config/module.config.php :

// ...

'view_manager' => [
    'doctype'  => 'HTML5',
    'template_map' => [
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'dashboard/index/index'   => __DIR__ . '/../view/dashboard/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

I created the two separated layouts, one in module/Application/view/layout/layout.phtml and the other one in module/Dashboard/view/layout/layout.phtml , logically it had to work, but it doesn't, it always call the Dashboard layout even for the Application . I was wondering, how to use separated layouts for each module?

I had the same issue on a previous ZF2 project. The problem is that you use the same 'layout/layout' identifier for both modules and during config merging, one is lost.

The idea is to give different names for identifiers, and to use an abstract controller which will permits to change the layout. And on the dispatch event, you attach a function wich will set the layout for your module:

Module.php (of your main module)

public function onBootstrap($e)
{
    $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', function($e) {
    $controller = $e->getTarget();
    $controllerClass = get_class($controller);
    $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
    $controller->layout($moduleNamespace . '/layout');
    }, 100);
}  

And in module.config.php of all modules using a different layout (for example Dashboard ):

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => array(
        'Dashboard/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'Dashboard/index/index' => __DIR__ . '/../view/application/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

And it should be OK. Else you can also use other party code, like EdpModuleLayouts , but it is no more maintained... The good point of my solution is that you should understand what you do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM