Zend Framework: module specific layout

In this article I will show you how set up module specific layouts. Many people find it confusing that module resources like layouts are always loaded, regardless of the module requested. The reason for this is that at the time of bootstrap, the request is not yet determined and the application does not know what module was requested. To set up resources like layouts, databases, translations etc. for specific modules ONLY when that module is requested, your best bet is a front controller plugin or action helper.

Usage example

Default layout settings in application.ini:

resources.layout.layout = "default"
resources.layout.layoutPath = APPLICATION_PATH "/views/layouts"

Then, if you use my ModuleSetup resource as described in module config, you can set the module layout options in the module.ini for that module:

resources.layout.layout = "modulename_default"
 
; this layoutpath is relative to the directory of the module itself
; in this case: application/modules/modulename/views/layouts
resources.layout.layoutPath = "views/layouts"

or, you can set the module config in your application.ini normally, as described in the Zend Framework reference:

modulename.resources.layout.layout = "modulename_default"
 
; this layoutpath is relative to the directory of the module itself
; in this case: application/modules/modulename/views/layouts
modulename.resources.layout.layoutPath = "views/layouts"

* note: you can set all options for module layouts sources the same way as for the application layout resource. If you don’t specify a different layoutPath, the standard path is used

The result is that when the module ‘modulename’ is requested, the layout ‘modulename_default.phtml’ in the directory ‘application/modules/modulename/views/layouts’ is used. Otherwise, the main layout is used.

Code

class Rexus_Controller_Plugin_RequestedModuleLayoutLoader
    extends Zend_Controller_plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $config     = Zend_Controller_Front::getInstance()
                            ->getParam('bootstrap')->getOptions();
        $moduleName = $request->getModuleName();
 
        if (isset($config[$moduleName]['resources']['layout']['layout'])) {
            $layoutScript = $config[$moduleName]['resources']['layout']['layout'];
            Zend_Layout::getMvcInstance()->setLayout($layoutScript);
        }
 
        if (isset($config[$moduleName]['resources']['layout']['layoutPath'])) {
            $layoutPath = $config[$moduleName]['resources']['layout']['layoutPath'];
            $moduleDir = Zend_Controller_Front::getInstance()->getModuleDirectory();
            Zend_Layout::getMvcInstance()->setLayoutPath(
                $moduleDir. DIRECTORY_SEPARATOR .$layoutPath
            );
        }
    }
}

To use this, drop the class above in your library and set the following in your application.ini:

resources.frontController.plugins.layoutloader = Rexus_Controller_Plugin_RequestedModuleLayoutLoader

If you want to know more about handling other types of resources with modules, check my post about module translation sources

9 Comments

  1. nicolas says:

    Hi

    Just needed to add:

    public function init() {
    Zend_Layout::startMvc();
    }

    to your class to make it work. I guess it can be some other issue, anyway it could be useful to post this.

    Thanks, great work with the modules thing for Zend Framework

  2. David Tay says:

    I ended up put this in the module.ini:

    resources.frontController.plugins.layoutloader = Rexus_Controller_Plugin_RequestedModuleLayoutLoader

    and then it finally worked.

  3. axm says:

    We would really profit a lot if you could post some Source Code from your implementations.

    I cannot get it running having a unique layout for each module, it is always refering to the main module.

  4. Mahmoud says:

    Hi,

    I was pulling the last few hairs out of my head when I stumbled upon this code along with the Module Config one.

    I really can’t thank you enough … Your’re awsome

    I’d like to suggest resetting the options of all the resources, any thoughts about that?

  5. [...] on freenode, so I thought I’d explore the idea a bit more.  There was this particular idea to use a plugin and a bit of application.ini/xml settings to configure it all.  But I’m a bit old school [...]

  6. eijunk says:

    Hi,

    Thank you for your helpful articles.

    To get my scripts work fine, I needed to change some parts of the example.
    And I hope my comment here would help those who are struggling.

    I use application.ini for module configuration, and its settings are as follows:

    ;default module
    default.resources.layout.layout = “layout”
    default.resources.layout.layoutPath = APPLICATION_PATH “/layouts/scripts/”
    ^^^^^^^^
    ;admin module
    admin.resources.layout.layout = “adminlayout”
    admin.resources.layout.layoutPath = APPLICATION_PATH “/modules/admin/layouts/scripts/”

    And I edited some lines of your LayoutLoader script as follows:

    //$moduleDir = Zend_Controller_Front::getInstance()->getModuleDirectory();
    Zend_Layout::getMvcInstance()->setLayoutPath($layoutPath);

    Since the layoutPaths of my configuration are defined with absolute paths, I needed to change the parameter for the setLayoutPath() method.

    Best wishes.

  7. Behrang says:

    Hi I’m newbie in zend so please tell me where exactly put the class with which file name and path I need it.
    thanks

Leave a Reply