Showing posts with label zend framework. Show all posts
Showing posts with label zend framework. Show all posts

Zend Framework modular design


Hi

This is a repost of the old post about the zend framework modular redesign .

Files and directories hierearchy

www/
 application/
  admin/
   controllers/
   forms/
   layouts/
   models/
   views/
  configs/
   application.ini
  default/
   controllers/
   forms/
   layouts/
   models/
   views/
  Bootrstap.php
 library/
  Zend/
 public/
  index.php

And here is the configs/application.ini file , I will list the most important lines .
This is for the modules , note that we have made the module directory , the Application Path itself , so you can put your modules inside application directory as we did in the directories hierarchy .

includePaths.library = APPLICATION_PATH "/../library"

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

appnamespace = "Application"
resources.frontController.moduleDirectory = APPLICATION_PATH 
resources.modules[] = 


and in the Bootstrap file , you can define _init function as you can , the most important one is the autoload function , here is Bootstrap.php file
here you are loading the two namespace you have ( default and admin ) .

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(
                        array('namespace' => 'Default',
                            'basePath' => dirname(__FILE__) . '/default',
                        )
        );
        
        $autoloader2 = new Zend_Application_Module_Autoloader(
                        array('namespace' => 'Admin',
                            'basePath' => dirname(__FILE__) . '/admin',
                        )
        );
        
        $application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        Zend_Layout::startMvc();
        $bootstrap = $application->getBootstrap();

        Zend_Session::start();
        $db = new Zend_Db_Adapter_Mysqli($bootstrap->getOption("database"));
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        return $autoloader;
    }
}
That's it , feel free to ask any questions .

Zend Framework Modular Design

This is an old post , please refer to the new one here .
Introduction
In this tutorial , I will show you how to make a module based web application using zend framework .

Application Structure 

This is the default application structure for a zend framework web application .
The simple change you can see is modules directory inside application directory , which will contain the different modules of your application
- application
- configs
- controllers
- modules
- views
- Bootstrap.php
- library
- public

Module Structure
Inside modules directory you will put a directory for each module you will make .
I will make an admin module so my modules directory will be like the following .
- configs
- controllers
- modules
- admin
- views
- Bootstrap.php

configs/application.ini
Here is the configuration you should put to enable modules structure in your zend framework application.
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =

Bootstrap.php
And this is the main Bootstrap file


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    protected function _initAppAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => 'Default',
                    'basePath' => dirname(__FILE__),
                ));
        return $autoloader;
    }
}
Module Bootstrap.php
And this is bootstap file of the admin module

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {

}
Pay attention that this class extends Zend_Application_Module_Bootstrap file not Zend_Application_Bootstrap_Bootstrap like the main bootstrap file .

Module Controller
This is an example of controller in the module
/application/modules/admin/controllers/IndexController.php



class Admin_IndexController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        
    }

}

Add new library in Zend Framework

In case you have your own library , and you want to use beside the Zend Framework library , all you have to do is to place your library directory beside Zend library as the following hierarchy


application
library
---- Zend
---- Php
public

Then in /application/configs/application.ini you should add the following line
autoloaderNamespaces.php = "Php_"

Of course you should name your classes as Zend convention , to load them easily .
For example if you have a class named Php_Class_Test , so its path will be Php/Class/Test.php