ZF2: Configure a layout for each module with EdpModuleLayouts

I am newly acquainted with ZF2 and played with EdpModuleLayouts to configure different layouts for each module. For someone with very limited exposure to ZF2, the instruction provided in EdpModuleLayouts is not quite enough. I've spent about an hour making this simple thing work and sharing my experience with others who may run into similar challenges.

Here is what you'll have to do to make EdpModuleLayouts work:

1. Install EdpModuleLayouts module in the "vendor" folder.

# cd vendor;
# git clone https://github.com/EvanDotPro/EdpModuleLayouts.git

2. Enable EdpModuleLayouts module in the application.config.php file located in "config" folder.

    'modules' => array(
        'EdpModuleLayouts',
        'Application',
    ),

3. Here is a fuzzy instruction from EdpModuleLayouts instruction..."In any module config or autoloaded config file simply specify the following:"

array(
    'module_layouts' => array(
        'ModuleName' => 'layout/some-layout',
    ),
);

You may add "module_layouts" array shown above in any of the Module's module.config.php file or config/autoload/{,*.}{global,local}.php file as the module.config.php and {global,local}.php files are merged by the ZF2. The trick is that you do now want to overwrite the "layout/layout" template map defined in the "view_manager" of different modules. If you have a duplicate "layout/layout", the config file from the latest module included in the application.config.php will be used.

So, how do we configure the layout portion?

We'll define the layout of the modules in the module.config.php of the Application module. Comment out the "view_manager" from the remaining module.config.php files:

    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'XHTML1_TRANSITIONAL',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'admin/layout'    => __DIR__ . '/../../Admin/view/layout/admin-layout.phtml',
            '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' => array(
            'application' => __DIR__ . '/../view',
            'admin' => __DIR__ . '/../../Admin/view',
        ),
    ),

    'module_layouts' => array(
                'Application' => 'layout/layout',
                'Admin' => 'admin/layout',
    ),

In the configuration above, we are setting two layouts: "admin/layout" for the Admin Module and "layout/layout" for the Application module.

References

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment