Zend Framework , wamp and bootstrap!

28 08 2009


I’m starting on the zend framework and, like many people ( I hope -.-’ ), I had some problems with the bootstrap and the Zend_Application API. I spent almost 3 hours today trying to understand the source of the problem that I found and yes, it was REALLY stupid!

I’m developing a web application with this simple file structure:

file structure

file structure

As you can see I have a modules directory and an “admin” module. This app consists in a web store for motor electrical parts and it will have and administration part that will allow the client insert new products, manage stock, manage clients, manage requests, etc, and the default module is for searching products and present products basically. The main problem for me was how could I setup zend in order to recognize my module and after some googling  i found some answers:

So basically you can incorporate your modules with the rest of the application with the Zend_Controller_Front using setControllerDirectory, addControllerDirectory or through the Zend_Application Resources but basically it is the same as with the Zend_Controller_Front however you use the Zend_Application and the config file.

So where would you do that? Simple, in your application bootstrap file!

Let’s see some examples:

  • Let’s do it with the Zend_Front_Controller API. In your bootstrap if you make one function protected and with the name starting with _init then it will be executed automatically as if it was an resource ( check out ) :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSetupModulesDirectories(){
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory(array(
'default' => APPLICATION_PATH . '/controllers',
'admin' => APPLICATION_PATH . '/admin/controllers'
));
}
}

  • The next example is basically the same but not using directly the Zend_Controller_Front class, instead it uses the frontController  resource!

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSetupModulesDirectories(){
self::$frontController = $this->getResource('FrontController');
self::$frontController->setControllerDirectory(array(
'default' => APPLICATION_PATH . '/controllers',
'admin' => APPLICATION_PATH . '/admin/controllers'
));
}
}

  • Now using addControllerDirectory():


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSetupModulesDirectories(){
$front = $this->getResource('FrontController');
$front->addControllerDirectory(APPLICATION_PATH . '/admin/controllers','admin');
}
}

  • And for the last but not the least let’s use the application.ini to load the modules directory and plus set up resource autoloaders for models, forms, services, etc:

Open up your configuration file (application.ini) and add this:
resources.modules = ""
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

This will initialize the module resource plugin and set the modules directory! Next what we need to do is create a bootstrap file for your module directory (application/modules/admin/Bootstrap.php).

It must be something like this:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap{
}

Pay attention the the class name, it starts with Admin_, this is named the PEAR convention for class names, don’t forget about it!

And finally in the application bootstrap class just add:
protected function _initAutoload()
{
new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
));
}

This will initialize the resource autoloaders for the default module. (An explanation of autoloaders here)

I hope this can help you and give you and idea with modules and boostraping. Just a quick tip, be carefull with typing errors, do not be like me! I’ve been 3 hours around one problem and the real problem was that I created a virtual host on apache (working with wamp) and there I’ve specified that the directory was for C:\wamp\www\mypage\public however the real name of the directory was Mypage with a big M and this was enough to let htaccess block all the request for http://mypage/index/ or http://mypage/admin ….

Happy coding and happy personal life xP


Actions

Information

Leave a comment