06 July 2009

Bootstrapping the Zend Framework

Zend_Application will automatically bootstrap the file, but that doesn't mean we can't go and put in our own little code there.

The bootstrap file is now a class. It extends Zend_Application_Bootstrap_Bootstrap. To run a method within the file upon bootstrap, the function must start with _init, e.g.

 function _initViewHelpers()

As Zend_Application will load the settings from the application.ini file itself, the bootstrap can actually be empty when you start your project, although it must still exist.

My bootstrap file contains the following snippet:

    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath'  => APPLICATION_PATH,
        ));
        return $autoloader;
    }

 function _initViewHelpers()
 {
  $this->bootstrap('layout');
  $layout = $this->getResource('layout');
  $view = $layout->getView();  
  $view->doctype('XHTML1_STRICT');
  $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Zend_View_Helper'); 
 }

The autoloader removes the default module, meaning the namespace is removed and my models, etc, don't require a Default_Model_Example but is instead Model_Example.

The viewhelper basically sets the layout and the doctype, as well as the viewhelper path, as that is not picked up by default by Zend_Application.

There is much more you can do in the bootstrap, i.e. some people set their timezone in the bootstrap, but that's all for now.

Take care!
Tjorriemorrie

No comments:

Post a Comment