I am working on a small API for my own backend and i wanted a modular architecture. I have done this before using the Zend Framework. But this time i wanted a bit more control while loading the modules. And adding a Bootstrap class would seem like a good option. The only example i could find involved loading all bootstraps on every request. Which doesn’t seem like a good idea. So after reading through the Manual and some blog posts. I decided to give it s shot my self.
The structure i want looks like this.
The application.ini file has the following contents:
- includePaths.library = APPLICATION_PATH “/../library”
- bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
- bootstrap.class = “Bootstrap”
- resources.frontController.moduleDirectory = APPLICATION_PATH “/modules”
- resources.modules = “default”
- resources.modules = “admin”
includePaths This sets the applications local library location. Any shared code for this application goes here.
bootstrap.path & class Define the location and type of the Bootstrap class.
resources Define the modules location and create a list of modules.
The main Bootstrap class
Load the config parameters for this application and set some debugging settings if needed.
We need autoloading here because we are using a class from the application library. Right now this causes a problem. A notice is thrown
Warning: include_once(FrontController.php) [function.include-once]: failed to open stream: No such file or directory in Zend/Loader.php on line 147
Setup the controller to register the Bluess_Modules_Loader plug-in. And set the prefixDefaultModule parameter so we can prefix the default module controllers as well. Just for the sake of consistency. The Bluess_ namespace is part of my API. And can be changed at will.
Now the last method. which is a bit weird. And i am probably missing a key factor here. But if this method resource is not declared only the default module functions. When declared empty all modules function as they should. This would indicate that this method could be used to load the modules. But i haven’t found a way to achieve this yet. Except for loading all modules in a row. Which makes no sense for this purpose. So we leave it empty.
The most important part here is the controller plug-in. This will be the place where module bootstraps are called from.
Setup the plug-in by passing the applications module list.
The dispatchLoopStartup method will be called on every request and will do the magic. Based on the current module name we create a new Zend_Application with the current modules config file module.ini. And we bootstrap it.
Now setup the default module. Once this is done it’s a nice example for further modules. Make sure the module has it’s own layout set.
default.resources.layout.layout = “default” default.resources.layout.layoutPath = APPLICATION_PATH “/modules/default/layout”
Setup the modules bootstrap and use it to set the modules model location.
That’s all. Now make sure your layout is set correctly and the controllers are prefixed
It took me a while to get this working like i had it in my mind. But it’s going the right way. If your interested in a working copy. You can download one here.
Matthew has a nice post about some do’s and don’ts concerning module based applications