Posts tagged: layoutPath

Zend Framework Per-Module based settings

By , Friday 1st January 2010 10:40 pm

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:

  1. 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"
  2. 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);
    	        }
        	}
        }    
    }
  3. 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());
    	}
    ...

Panorama Theme by Themocracy

4 visitors online now
2 guests, 2 bots, 0 members
Max visitors today: 7 at 04:53 pm UTC
This month: 16 at 07-08-2017 06:57 am UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC