Admin sub-modules in Zend Framework

Modules in Zend Framework essentially allow us to organise a collection of controllers into sub-folders, giving URL to filesystem mapping such as:

domain.com/user/register -> app/modules/user/RegisterController.php

While useful when we need to expand our URLs (and organisation of code) beyond one set of controllers, there are a few things they don’t currently solve which I think would make them first-class citizens within ZF.

I quite frequently find myself wanting some form of sub-modules, usually in admin systems where one “admin” module just doesn’t cut it for larger sites. Without modules we’re stuck with an AdminController.php which obviously gets very messy, very quickly. With modules, we can have an “admin” module with as many controllers as we wish. This is fine, until a site needs to manage lots of discrete elements.

For example, imagine a site with a CMS, user management system and a custom system to search for wines. While you could have a CmsController, UserController and WinesController in your “admin” module, it’s likely Cms and User would benefit from splitting out into multiple controllers of their own.

There’s also the issue of modularity. If I design a user management app which I want to re-use, it’s easier if I can install this into a folder (or SVN external it). If reusable components are mixed in the same folder they become harder to maintain and keep up to date.

In one attempt to solve this I created an Admin Modules bootstrap resource, which hijacks the domain.com/admin route and enables you to add admin modules to this URL.

To enable an admin module simply create a folder in your application folder called admin-modules. Within here simply place each module. Mapping works as follows:

domain.com/admin/
-> application/admin-modules/default/controllers/IndexController.php

domain.com/admin/user/
-> application/admin-modules/user/controllers/IndexController.php

Within each admin module the normal ZF rules for URL routing applies. For convenience my bootstrap resource also sets up an ID route:

domain.com/admin/user/groups/view/4
-> application/admin-modules/user/controllers/GroupsController.php / viewAction

passing a param called “id” with a value of 4.

The filesystem structure works as so:

application/
            admin-modules/
                          default/
                                  controllers/
                                  etc..
                          user/
                               controllers/
                               etc..

So admin-modules basically works like a copy of the main modules folder. Just for admin-only modules.

To enable this simply add the following line to your Zend_Application application.ini config file:

; Register a custom Bootstrap resource path (needs to be in your include path)
pluginPaths.S24_Application_Resource = "S24/Application/Resource"

; Enable admin modules
resources.adminModules.enable = []

Note: Please now see the sub-modules system for an improved way to implement this.

5 Replies to “Admin sub-modules in Zend Framework”

  1. hi Simon,
    in your code, i add some modules: acl, comment => it don’t work, because it’s sort a-z before D “default” module. (i add some module like product, user then work)

    anh how can i layout for that modules???

  2. Truc Le – yep, you’re completely right. The ‘default’ admin route is more generic than the route for specific modules like ‘acl’ or ‘cms’ so it overrides it. We found the same issue last week so I’ve fixed the bug and refactored the code into a more generic sub-module system so it supports root folders other than just ‘admin’.

    Take a look at – http://www.simonrjones.net/2010/06/sub-modules-in-zend-framework/

Comments are closed.