ZF2: Configure a layout for each module with EdpModuleLayouts

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

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(

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

    '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 module. If you have 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 Admin Module and "layout/layout" for Application module.

EdpModuleLayouts by Evan Coury.