Thursday, June 27, 2013

Modular applications with Zend Framework 1.11

STEP 1 –

Create folder with name “modules” under your “application” folder.

STEP 2 –

Then under “modules” folder you can create folder with your module name for example suppose we are going to develop separate module for Administrator so module name should be “admin”.

Now your file structure should be something like this.




STEP 3 –

Rest of programming and folder and file structure should be same we have on our default setup of zend framework  on “application” folder.

But now your class name should be with prefix of module name.

For example – Our example module is “admin”

Then class name should be like  this–

Admin_UserController, Admin_Model_UserModel, Admin_Form_LoginForm, etc.

But file should be save with name “UserController.php” if we taking an example of controller.


STEP 4 –

We now have to make changes on “application.ini” file to make sure Zend is aware of that module
application.ini :
Append following lines.

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "default"
resources.modules[] = ""



STEP 5 –

Again, the naming is important; the class name must be [module name]_Bootstrap and it must extend Zend_Application_Module_Bootstrap.Finally add a Bootstrap class to the module:

It must be stored in a file called Bootstrap.php within the root of the module.

<?php

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
       public function _initAutoload()
        {
            // Each module needs to be registered...
            $modules = array(
                'Admin',               
            );

            foreach ($modules as $module) {
                $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => ucfirst($module),
                    'basePath'  => APPLICATION_PATH . '/modules/' . strtolower($module),
                ));
            }

            return $autoloader;
        }
}


The module is now ready to go.

No comments:

Post a Comment