@shakedko
IF AN EXPERT SAYS IT CAN'T BE DONE GET ANOTHER EXPERT.
- DAVID BEN-GURION

Zend Framework - Working with Modules

Zend Framework has much more power then you really know, and today we will talk about Zend Framework working with modules.

Lets just start:

How we define modules and why we need them?

Modules are very important, fully power and useful tool. As I see it, its helps us to create mini-application inside our main application. While using our module, we have the ability to use its power on our main application and to replace it any time we want without hurting our code. Another important issue is the ability of flexibility which means that we can detach our module from one program and use it on another program.

While creating  modules you need to know few things which will help you to start fast. 

  • For creating new module with zf tool, use the following command:

zf create module name, e.g: zf create module Users.

Note: Before using this command, you will have to be on your project (ZF) directory, e.g:

cd /var/www/html/skoproject/ | zf create module Users
  • Know your enemy ;). Its important to know modules folder structure which you can see here. Note that you can use controllers/ folder as well. Full folder structure will be:

forms/
models/
DbTable/
mappers/
plugins/
services/
views/
helpers/
filters/
controllers/
Bootstrap.php
  • After creating your module and your desired directory structure, you will have to update your main Bootstrap.php(/path/to/project/application/Bootstrap.php) to know your module(s):

//Don't forget to bootstrap the front controller as the resource may not been created yet...
protected function _initAppModules(){
    $this->bootstrap("frontController");
    $front = $this->getResource("frontController");
    /*adding modules dir to defult route, see:
    http://framework.zend.com/manual/1.11/en/zend.controller.modular.html#zend.controller.modular.directories*/
    $front->addModuleDirectory(APPLICATION_PATH . '/modules');
}
  • When using modules, you probably should know that you have some "default" module that surprisingly  will called "Default"

  • While using your module, you will have to know files & classes syntax. All of your classes will be: ModuleName_DirectoryName_FileName, for example, when you are working with Users module, on Register form you will use:Users_Form_Register and you will find this class under: APPLICATION_PATH . '/modules/users/forms/Register.php'

  • When working with "Default" module, you won't have to name your classes with ModuleName, which means that you will work with:

    Form_Register under APPLICATION_PATH . '/modules/default/forms/Register.php'.I still advice to use your naming convention the same as the others.

  • Full naming convention may be found at Zend Framework documentation
  • You may use different configuration file for your module instead of your main application config file. Use it under APPLICATION_PATH . '/modules/ModuleName/config/application.ini , and don't forget to init your settings inside the module's Bootstrap.php

To sum-up, I`v read some helpful articles about Zend Framework and modules, and I think you should read them as well:

Matthew Weier O'Phinney - About Modules

Creating and Managing Zend Framework Modules

Zend Framework Store Front example project with modules

 

Hope I it helped.