Js Lim
Js Lim

Reputation: 3705

ZendFramework 2 - Error in loading layout for different modules

In ./config/application.config.php

return array(
    'modules' => array(
        'Application',
        'Admin',
    )
    ...

I have 2 separate set of layouts, ./module/Application/view/layout/layout.phtml and ./module/Admin/view/layout/layout.phtml

In ./module/Admin/config/module.config.php

...
'template_map' => array(
    'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
    'header'        => __DIR__ . '/../view/layout/header.phtml',
    'footer'        => __DIR__ . '/../view/layout/footer.phtml',
    'paginator'     => __DIR__ . '/../view/layout/paginator.phtml',
    'error/404'     => __DIR__ . '/../view/error/404.phtml',
    'error/index'   => __DIR__ . '/../view/error/index.phtml',
)
...

In ./module/Application/config/module.config.php

...
'template_map' => array(
    'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
    'header'        => __DIR__ . '/../view/layout/header.phtml',
    'footer'        => __DIR__ . '/../view/layout/footer.phtml',
    'paginator'     => __DIR__ . '/../view/layout/paginator.phtml',
    'error/404'     => __DIR__ . '/../view/error/404.phtml',
    'error/index'   => __DIR__ . '/../view/error/index.phtml',
)
...

Basically they are different set and some of the content are different. Unfortunately, both module only load the layout located in In ./module/Admin/config/module.config.php

I googled but didn't fount any solution that I want. Anyone has any idea on this?

Upvotes: 3

Views: 1930

Answers (1)

Sam
Sam

Reputation: 16455

You may be interested to know, what your configuration actually does. My Blog Post about this Topic, may interest you. Ultimately all Configuration files will be merged to one. The global configuration keys are not on a per-module basis ;)

To achieve your goal you should read Evan Courys Blog Post "Module-specific layouts in ZF2"

Evan provides a Module "EdpModuleLayouts" that makes things pretty easy. If however you only need one alternative Layout for your AdminModule, then i suggest you simply go with the example code of his Blog Post to set an alternate Layout for your AdminModule directy via your AdminModule/Module::onBootstrap

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

Not that this will set the layout to layout/admin. You would need to provide this key via your configuration:

'template_map' => array(
    'layout/admin' => 'path/to/admin/module/view/layout/admin.phtml',
)

Upvotes: 8

Related Questions