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());
    	}
    ...

Liked this post? Follow this blog to get more. 

8 Responses to “Zend Framework Per-Module based settings”

  1. Behrang says:

    Hi first thanks for your helpful tutorial but tell me where I create action helper with witch file name?
    behrang

  2. Hi Behrang, create the action helper with whichever name you like, I have my own ‘library’ with namespace ‘Pro’. You could just as easily call the class ‘SetLayoutPath’ and put it where ever you like, provided the code can find it and you extend the required classes

  3. shashi kanth says:

    Hi, iam confused with setting up module specific database settings… does anyone have an idea?

  4. I think this could easily be achieved usign the above pattern. Firstly, create options in your ini file along the files of:

    moduleName.db.user = ”
    moduleName.db.password = ”
    moduleName2.db.user = ”
    …etc…

    Then simply generate your database connection as you see fit (Zend_Db::factory() or similar) and stash it away in the registry or set it as the default adapter if using Zend_Db_Table (Zend_Db_Table::setDefaultAdapter()).

  5. Hi,

    This was really helpful, thanks. At the moment i am having real trouble implementing a module specific navigation plugin. Could this solution for layouts be applied to navigation?

    -Ben

    • Definitely, although you could also implement ACLs to do the same thing and have one large cached navigation configuration rather than one per module.

      • Hey Steven,

        Thanks for replying. I actually got my module based navigation working by modifying your solution above. I cant thank you enough, i was struggling on this issue for around 2 days.

        I have actually implemented an ACL system but when on the admin side of my system I do not want any of the main navigation to be visible, but still have access to the main side of the system. In my head module based navigation was the way forward, it just seemed a cleaner approach.

        Regards,
        -Ben

        • Glad you’ve got it working. Your solution makes complete sense.

          With the implementation I used I’d simply filter the navigation tree to the module I was in which meant that you only get the relevant pages. Navigation set up something like:

          – Master link
          — Admin module
          —- Index
          —- etc
          — Public module (default)
          —- Index
          —- etc

          So only displaying links from ‘admin module’ down in my admin module hides links I don’t want the user to see.

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

2 visitors online now
0 guests, 2 bots, 0 members
Max visitors today: 13 at 06:05 am UTC
This month: 16 at 18-07-2017 05:35 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC