How to get all your modules
Created: Last updated:
If you like to find all registered modules in your Zend application you have to use a little trick and use the getControllerDirectory() method. It will list the module directories and with it the controllers. For a more extensive answer please read on.
Working with modules in Zend Framework
If you have a little application in Zend Framework you will have all your controllers and actions as well as the models in the default application directory. If you write a larger application you want a modular approach and not add everything into this default application. The framework basically allows as many modules as you like.
As a side note: It is not obvious but internally the default application is a module, too.
Now lets see what happens with our modules when run our Zend web application. You start the whole ZF application with the following code:
- // Create application, bootstrap, and run
- $application = new Zend_Application(
- APPLICATION_CONFIG . '/application.ini'
If you look at the run() method in the bootstrap class you will find two calls; one goes to $front->getDefaultModule() and the other to $front->getControllerDirectory().
Now the big question: Aside from the default module, how do I get a list with the names of all my modules? There is no universal method for that, like getAllModules() in the framework. There is a surprisingly easy and actually not so unexpected answer to this question.
First some background
Knowledge is power so before I present the solution I provide some background. Feel free to jump ahead, though.
According to the documentation "Each module should be in its own directory and mirror the directory structure of the default module" and with this a controller directory and at least one controller class. First we see the hint that the default application is a module but second we need at least one controller class. When we look at the front controller we see two interesting methods: One is named addModuleDirectory($path) and the other getModuleDirectory($module).
Now first about the getter method. If you pass in the module name you get the path to that module. If you pass in [null] you will get the path of the active module, not a list! Bummer, I know! However, the add method gives a little hint.
Method addModuleDirectory($path) according to the doc "allows you to pass the name of a directory containing one or more module directories. It then scans it and adds them as controller directories to the front controller."
Two interesting things we have to note here: a) we can have more than just one module directory and b) did you notice the phrase "it then scans and adds them as controller directories to the front controller"? That's right, the front controller keeps track not so much of all the modules but all the controller directories in the framework! Sweet, ain't it?
The easy solution
The front controller has a method called getControllerDirectory($name). What is not obvious here is that the $name argument means a module's name. Every module should have its own controller directory and the front controller keeps track of them. Better yet, if you don't pass a parameter to the method you will not get the currently active module but a list of all controller directories, indexed by the module's name! The method returns an array and the index keys are your names of all your modules no matter where they are.
- // get the front controller instance
- $front = Zend_Controller_Front::getInstance();
- $cd = $front->getControllerDirectory();
- $moduleNames = array_keys($cd);
- // print the result
- array (
 => default
 => mo_admin
 => mo_blog
 => mo_catalog
So, that's how you get all your module names.
I have a getAllModules() method now. How and where you implement your method is up to you. Enjoy!