Module based layouts with Zend Framework
Current version of Zend Framework is completly based on a bootstrapping design pattern.
Many resource plugins are available, which can be managed via a config file. One of these is Zend_Application_Resource_Layout, which instantiates the layout for you based on the
resources.layout.layoutPath directive in your config.
While this worked fine for me with the initial setup, I wondered how I would have a differnt layout set for another module. Doing that login the in Layout Resource Plugin is no option, because bootstrapping happens before the application knows, which module will be executed.
So the correct way for me to solve the problem was to create a Controller Plugin which changes the layoutPath in the preDispatch method. I wanted to have this working with as little efford as possible, so I tried to stick to an implementation, which assumes the module based layout to reside in a very similar folder structure than what the default module is based on. In my case this would be a typical example:
/application/layouts/scripts/layout.phtml
vs
/application/admin/layouts/scripts/layout.phtml
So all I needed to accomplish was
- Replace application path with module path (which is set via resources.frontController.moduleDirectory)
- Check whether specific layout for module exists
- Change layoutPath for the according module
So after we went over the theory, this is the final controller plugin I’ve come up, which generically lets me add a /layouts/scripts/ folder to each of my modules and loads that. If no layout is found for that module, everything simply runs under the default layout.
<?php class Tarsonis_Controller_Plugin_ModuleLayout extends Zend_Controller_Plugin_Abstract { /** * Predispatch function for changing layout by requested module * * @param Zend_Controller_Request_Abstract $request */ public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); if($module == 'default') { return; } $layout = Zend_Layout::getMvcInstance(); if(!$layout->getMvcEnabled()) { return; } $moduleDirectory = Zend_Controller_Front::getInstance() ->getModuleDirectory($module); $moduleLayoutPath = str_replace(APPLICATION_PATH, $moduleDirectory, $layout->getLayoutPath()); if(!file_exists($moduleLayoutPath) && !is_dir($moduleLayoutPath)) { return; } $layout->setLayoutPath($moduleLayoutPath); } }
Wilkommen auf meiner Webseite
Ich weiß nicht, wie du den Weg hierher gefunden hast. Hallo auf alle Fälle.
Hier findest du Informationen zu allen Themen, die mit meinem Gewerbe zutun haben. Dazu gehören
- Webseiten an denen ich arbeite
- Informationen über mich selbst
- Informationen zu meinen Servern
- Programmierrelevante Themen
Viel Spaß nun beim stöbern auf meinen Seiten.

