Zend Framework modules

Posted in February 2012 by under zend-framework

Zend Framework Documentation is like using "My first is in Monkey but not in Giraffe...". There is probably no better example of this than trying to find documentation on how to start using modules in Zend Framework.

A search for Zend Framework modules led me to two likely documentation pages, one being this page where the comments quickly highlight the fact that this page contradicts other pages on how to lay things out and then links to another, seemingly similar page that explains things completely differently.

Unfortunately the second page, is still a pretty challenging read.

Having come out the other side and seeing that actually setting up Zend Framework to be modular is extremely flexible and simple to do, I am left wondering why it appeared so hard to begin with.

If you are trying to find a way to get started with modules in Zend Framework, here is how to do it:

1. Create a default module within your Zend Framework project

zf create module default

2. Move the controller/ view/ and models/ directories from application/ to application/modules/default/

3. Edit your controller class names to use the new namespace.

For example in modules/default/controllers/ErrorController.php rename the class ErrorController to Default_ErrorController.

4. Create application/modules/default/Bootstrap.php with the contents:

Default_Bootstrap extends Zend_Application_Module_Bootstrap

5. Add resources.modules[] = to the end of your production configuration in application/config/application.ini

Easy isn't it? So why does it take so long to read and dig about to understand the documentation?

When you want to create a new controller you need to use slightly different syntax to show which module the controller will belong to. So to add a login controller to the user module you would use

zf create controller login 1 user

And the URLs are affected so that your URLs now look like http://domain.com/module/controller/action/. If a module isn't specified, the Default module is used.