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:

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