FROM:http://mwop.net/blog/267-Getting-started-writing-ZF2-modules.html
During ZendCon this year, we released 2.0.0beta1 of Zend Framework. The key story in the release is the creation of a new MVC layer, and to sweeten the story, the addition of a modular application architecture.
"Modular? What's that mean?" For ZF2, "modular" means that your application is built of one or more "modules". In a lexicon agreed upon during our IRC meetings, a module is a collection of code and other files that solves a specific atomic problem of the application or website.
As an example, consider a typical corporate website in a technical arena. You might have:
These can be divided into discrete modules:
Furthermore, if these are developed well and discretely, they can be re-used between different applications!
So, let's dive into ZF2 modules!
In ZF2, a module is simply a namespaced directory, with a single "Module" class under it; no more, and no less, is required.
So, as an example:
modules/ FooBlog/ Module.php FooPages/ Module.php
The above shows two modules, "FooBlog" and "FooPages". The "Module.php" file under each contains a single "Module" class, namespaced per the module: FooBlogModule
andFooPagesModule
, respectively.
This is the one and only requirement of modules; you can structure them however you want from here. However, we do have a recommended directory structure:
modules/ SpinDoctor/ Module.php configs/ module.config.php public/ images/ css/ spin-doctor.css js/ spin-doctor.js src/ SpinDoctor/ Controller/ SpinDoctorController.php DiscJockeyController.php Form/ Request.php tests/ bootstrap.php phpunit.xml SpinDoctor/ Controller/ SpinDoctorControllerTest.php DiscJockeyControllerTest.php
The important bits from above:
Again, the above is simply a recommendation. Modules in that structure clearly dileneate the purpose of each subtree, allowing developers to easily introspect them.
Now that we've discussed the minimum requirements for creating a module and its structure, let's discuss the minimum requirement: the Module class.
The module class, as noted previously, should exist in the module's namespace. Usually this will be equivalent to the module's directory name. Beyond that, however, there are no real requirements, other than the constructor should not require any arguments.
namespace FooBlog;class Module{}