One reader of my book recently asked me about creating modules in Zend Framework 2. Actually, every developer of a ZF2-based site earlier or later asks himself: “When should I create a new module?“.
When the site grows in size, you can benefit from using Zend Framework 2 feature called modules.
A module is an autonomous and reusable unit of your application. By default your application has the single Application module. And it is normal to put your models, views and controllers in this module.
You might need to create a new module when the following are true:
- If the functionality you are implementing is autonomous (has no or few dependencies on other modules of your application. For example, the user management functionality in theory can be separated in another module, because it does not depend on the rest of the application, but the application depends on it.
- If a module can be separated from your application and concurrently used in another project. For example the user management module can be reused in another project without changes (only its configuration may require changes).
In any case, real life is complex, and you should be guided by the intuition when it seems that it is necessary to create a new module. If you think the separating a functionality in a module gives you some significant benefits, then create the module.
At the same time, you should not be abused by creating a huge number of almost empty modules, it is better to combine related functionality in a single module.