In this ZF2 example you can set the layout to be what ever you want in each modules config, but I'd like to know if there's a way to share and organize the same layout and navigation between modules.
e.g. like a template, load default css, scripts and layout without duplicate layout/layout.phtml
and partial/navigation
for each module in my project, and then load specific css and scripts in my specific view using $this->headLink()->appendStylesheet()
and $this->headScript()->appendFile()
Asked
Active
Viewed 100 times
0

Community
- 1
- 1

wonderkarlo
- 11
- 1
-
1This is already the case; all module configuration is *merged* together. If you are currently defining a layout in each module under the `view_manager` key then whichever module loads last will set the layout for the whole application. The post you mentioned includes code to set a layout *per* module which is not what you want. – AlexP Jan 08 '16 at 11:53
-
If I understood correctly, it should be enough configuring a layout folder in one single module and 'view_manager'-> 'template_map' ->'layout/layout' in its module.config.php file to ensure that this configuration it's matched by all module of the whole application? I noticed that when you create a new app with ZF2 Skeleton Application and you add new module, the new module matches the layout configuration without any additional configuration. – wonderkarlo Jan 08 '16 at 12:21
1 Answers
0
The template_map
from your example can be defined in different models. ZF2 will merge all configs in one config array as explained here:
The
ModuleManager
‘sConfigListener
aggregates configuration and merges it while modules are being loaded.
So you can define a template map in module A and module B and in the config you will find both template maps merged. This means template names should be unique in the application otherwise you will end up overwriting them.
So in Module A module.config.php
:
'view_manager' => array(
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout.phtml',
'moduleA/list_view' => __DIR__ . '/../view/list.phtml',
'moduleA/info_view' => __DIR__ . '/../view/info.phtml',
),
)
So in Module B module.config.php
:
'view_manager' => array(
'template_map' => array(
'moduleB/list_view' => __DIR__ . '/../view/list.phtml',
'moduleB/info_view' => __DIR__ . '/../view/info.phtml',
),
)
You will end up with a merged template_map
containing all:
'layout/layout' => __DIR__ . '/../view/layout.phtml',
'moduleA/list_view' => __DIR__ . '/../view/list.phtml',
'moduleA/info_view' => __DIR__ . '/../view/info.phtml',
'moduleB/list_view' => __DIR__ . '/../view/list.phtml',
'moduleB/info_view' => __DIR__ . '/../view/info.phtml'
And now you will be able to access these views throughout your whole application.

Wilt
- 41,477
- 12
- 152
- 203