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:


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:
     * 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 =
  3. And lastly boostrap the action helper:
         * Sets up layout scripts on a per-module basis
        protected function _initLayoutHelper()
    	    $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?

  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 = ”

    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?


    • 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.


        • 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

1 visitors online now
0 guests, 1 bots, 0 members
Max visitors today: 6 at 03:27 am UTC
This month: 15 at 10-10-2017 02:55 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC