I’ve created a followup to this post which requires less configuration, please see Module Based Layout – Zend Framework.
When using the zend framework with modules, its obvious that if you’re running various (sub-)sites off the same application you don’t necessarily want the same layout scripts for each part. I decided to go with the following site structure:
/Application /controllers ... /models /modules /default /controllers /layout /scripts /views /scripts /anotherModule ... /scripts
The problem was setting up the layout scripts on a per-module basis. The answer came through using an Action Helper. Setting up the layouts on a per-module basis involves three steps:
- Application.ini (or similar configuration setup):
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts" default.resources.layout.layoutPath = APPLICATION_PATH "/modules/default/layouts/scripts" member.resources.layout.layoutPath = APPLICATION_PATH "/modules/member/layouts/scripts" affiliate.resources.layout.layoutPath = APPLICATION_PATH "/modules/affiliate/layouts/scripts"
- Create your Action Helper:
<?php /** * Sets the layout path on a per-module basis * * @author Lloyd Watkin <lloyd@evilprofessor.co.uk> * @since 2010-01-01 */ class Pro_Controller_Action_Helper_SetLayoutPath extends Zend_Controller_Action_Helper_Abstract { /** * Sets layout path based on module */ public function preDispatch() { $module = $this->getRequest()->getModuleName(); if ($bootstrap = $this->getActionController() ->getInvokeArg('bootstrap')) { $config = $bootstrap->getOptions(); if (isset($config[$module]['resources']['layout']['layoutPath'])) { $layoutPath = $config[$module]['resources']['layout']['layoutPath']; $this->getActionController() ->getHelper('layout') ->setLayoutPath($layoutPath); } } } }
- And lastly boostrap the action helper:
... /** * Sets up layout scripts on a per-module basis */ protected function _initLayoutHelper() { $this->bootstrap('frontController'); $layout = Zend_Controller_Action_HelperBroker::addHelper( new Pro_Controller_Action_Helper_SetLayoutPath()); } ...