Sejanus
Sejanus

Reputation: 3323

Zend_Layout for modules

How can I make every module have it's own layouts directory?

I.e. when I don't have any modules my layout entry in config file looks like this:

resources.layout.layoutPath = APPLICATION_PATH "/layouts"

I try entering i.e.

; Layout directory for admin module
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"

Where admin is module name; but it doesn't work. For some strange reason ZF looks for module layouts in /module/admin/views/scripts directory.

I also have a separate module.ini config file for every module as per this tutorial, alas layout path there gets ignored as well. Also I've been trying to follow this modules layout tutorial but it didn't work, I guess due to differences in ZF versions (tutorial is rather old). So I don't know what else to do

Upvotes: 0

Views: 2077

Answers (5)

As of Zend Framework 1.12 (Haven't tested it on previous releases):

  1. Create your modules
  2. Initialize the layout in your prefered fashion. For example in application.ini as zend tools does it:

    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
    
  3. Create a layout inside each module with the default layout name inside the modules path to "views/scripts/" for example "application/modules/default/views/scripts/layout.phtml"

Don't forget to create one for the default module as it will be your fallback layout!

DO NOT create the default layout inside /application/layouts/scripts or this won't work

You are ready to run!

When Zend_Layout doesn't find the default layut it will look into the modules folders for it.

If you need some extra tweaking you may create a plugin and assign it to the layout object itself. For example, inside application.ini:

resources.layout.pluginClass = "MyLibrary_Controller_Plugin_Layout"

...or in the Bootstrap:

Zend_Layout::getMvcInstance()->setPluginClass("MyLibrary_Controller_Plugin_Layout");

Cheers!

Upvotes: 0

Ololo
Ololo

Reputation: 1097

Using plugin from the tutorial you are talked about:

class My_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'])) {
            Zend_Layout::startMvc($config[$moduleName]['resources']['layout']);
        }
    }
}

application.ini

resources.frontController.plugins.layoutloader = My_Controller_Plugin_RequestedModuleLayoutLoader 

module.ini:

resources.layout.layout = "Admin"
resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts"

Working fine.

Upvotes: 2

Phil
Phil

Reputation: 164760

Have a look at this gist - https://gist.github.com/891384

This uses a combination of

  • Action helper to inspect the requested module and given a matching configuration, change the layout's layout and layoutPath properties in the preDispatch hook
  • Application resource plugin to capture module layout options, inject them into the above helper and add it to the helper broker

Upvotes: 2

Andy Baird
Andy Baird

Reputation: 6208

A slightly alternate method to Ololo recommendation (which is a great way to do it)..

class YourApp_Controller_Plugin_Modulelayout extends Zend_Controller_Plugin_Abstract
{
     public function routeShutdown(Zend_Controller_Request_Abstract $request)
     {
          $module = $request->getModuleName();
          if ($module != 'default')
          {
              if (file_exists(APPLICATION_PATH . '/layouts/' . $module . '.html')) {
                  Zend_Layout::getMvcInstance()->setLayout($module);
              }
          }
     }
}

Place this controller plugin in /library/YourApp/Controller/Plugin/Modulelayout.php Then save your module layouts as the module name in your layouts folder (E.g., /layout/admin.phtml). If it does not find a layout for that module, it will default back to layout.phtml or whatever you originally set it to.

Upvotes: 2

Perfection
Perfection

Reputation: 199

Happened to me too I got around it by using this line in my controller (I created a init function)

Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/modules/admin/layouts')); 

Upvotes: 1

Related Questions