Understanding the Zend Framework bootstrap
ZF Version: 1.10.8
The objetive of this article is to explain how Zend Framework bootstrap works and, at a second part, show samples of bootstrap for common components.
The bootstrap class is used to load common components or resources that are used by all or most of your controllers, views etc. On “non-framework” applications it is common to have something like includes/common.php to “bootstrap” your application.
At this place you usually:
- Connect to database;
- Start session;
- Load config files;
- Load common libraries;
On ZF bootstrap, the idea is almost the same. Let’s now, try with an example. Consider the following empty bootstrap:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
}
With ZF, all bootstrap methods starts with _init and are protected. Example:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initMonkeyAttack()
{
// Do your thing here
}
}
This is just an example and you should always use things that describe what the method is doing. Unless you are really starting a monkey attack ( in this case, call me! ) change to something else.
Anything that starts with _init will be executed before application bootstrap.
What if i need to run method1 before method2?
It is common to require that one method is only called after another one. Example: the method that starts session should only start after the method that loads database, since your sessions are stored on database.
In this case, you can use $this->bootstrap( ‘nameOfTheMethod’ ); which will load a resource before the current one. Example:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDb()
{
// Start database connection here
}
protected function _initSession()
{
// Bootstrap db first
$this->bootstrap( 'db' );
// Now start session
}
}
That’s it. This is the first part of this article. At the second part i will show bootstrap for common components like Zend_Db, Zend_Session, Zend_Translate, Zend_Locale etc.


6 February, 2012 at 4:14
Thanks man. Your post helped me a lot in understanding Zend Bootstrap basics.