Zend Framework Bootstrap file class

by Gabi Solomon

After starting working with zend framework i found all sorts of examples of bootstrap files, but a lot of them seem pretty messy to me since all the calls to zend components were basically thrown in there.

So after a lot of looking around i came up with a bootstrap files that is more to my liking. I found the initial class on a blog post when i was looking around but i don’t remember where, so sorry for not giving it the credit.

First i will show you my index file.
require ‘./init.php’;


Pretty simple isnt it :D . And now the init file.

// Add /library and /application directory to our include path
$siteRootDir = dirname(__FILE__);

define(‘APPLICATION_PATH’, $siteRootDir . ‘/application’);

. APPLICATION_PATH . ‘/modules/default/controllers’ . PATH_SEPARATOR
. $siteRootDir . ‘/library’ . PATH_SEPARATOR
. get_include_path()

require APPLICATION_PATH . ‘/bootstrap.php’;

Ok now lets go into the bootstrap :) . I will firs just paste it here and then write some explinations to it.


function __autoload($className) {
require $className = str_replace(‘_’, ‘/’, $className) . ‘.php’;

class Bootstrap{

public static $frontController = null;

public static $root = ”;

public static $registry = null;

public static function run(){

$response = self::$frontController->dispatch();

public static function prepare(){




public static function setupEnvironment(){
error_reporting(E_ALL ^ E_NOTICE);
ini_set(‘display_errors’, true);


self::$root = dirname(dirname(__FILE__));

$configType = (isset($_SERVER['SERVER_NAME']) && ($_SERVER['SERVER_NAME'] == ’′ OR $_SERVER['SERVER_NAME'] == ‘localhost’)) ? ‘development’ : ‘production’;
define(‘APPLICATION_ENVIRONMENT’, $configType);

define(‘HTMLPURIFIER_PREFIX’, self::$root . ‘/library’);

public static function setupRegistry() {
self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);

public static function setupConfiguration() {

$config = new Zend_Config_Ini(
self::$root . ‘/application/config/main.ini’,

self::$registry->configuration = $config;

//save $siteRootDir in registry:
self::$registry->set(‘siteRootDir’, self::$root );
self::$registry->set(‘applicationRootDir’, self::$root . ‘/application’ );

self::$registry->set(‘siteRootUrl’, ‘http://’ . $_SERVER['HTTP_HOST'] );

public static function setupFrontController(){
self::$frontController = Zend_Controller_Front::getInstance();

self::$frontController->addModuleDirectory(self::$root . ‘/application/modules’);

self::$frontController->setParam(‘registry’, self::$registry);

self::$frontController->setParam(‘env’, APPLICATION_ENVIRONMENT);

$response = new Zend_Controller_Response_Http; // Set default Content-Type
$response->setHeader(‘Content-Type’, ‘text/html; charset=UTF-8′, true);

public static function setupErrorHandler() {

self::$frontController->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(
‘module’ => ‘default’,
‘controller’ => ‘error’,
‘action’ => ‘error’)

$writer = new Zend_Log_Writer_Firebug();
$logger = new Zend_Log($writer);

public static function setupController(){
// place to put in your Controll Action Helpers
// ex: Zend_Controller_Action_HelperBroker::addHelper(new GSD_Controller_Action_Helper_AuthUsers());

public static function setupView() {

$view = new Zend_View(array(‘encoding’=>’UTF-8′));
$view->addHelperPath(‘GSD/View/Helper’, ‘GSD_View_Helper_’);

$viewRendered = new Zend_Controller_Action_Helper_ViewRenderer($view);

‘layoutPath’ => self::$root . ‘/application/layouts’,
‘layout’ => ‘default’

public static function sendResponse(Zend_Controller_Response_Http $response){

public static function setupDatabase(){
$config = self::$registry->configuration;

$db = Zend_Db::factory($config->db->adapter, $config->db->toArray());

$profiler = new Zend_Db_Profiler_Firebug(‘All DB Queries’);

$db->query(“SET NAMES ‘utf8′”);

self::$registry->database = $db;

$frontendOptions = array(
‘automatic_serialization’ => true

$backendOptions = array(
‘cache_dir’ => self::$root . ‘/data/cache/db_table’

$cache = Zend_Cache::factory(‘Core’,

// Next, set the cache to be used with all table objects

public static function setupSessions(){

// Now set session save handler to our custom class which saves the data in MySQL database
$sessionManager = new GSD_Session_Manager();
‘gc_probability’ => 1,
‘gc_divisor’ => 5000

$defSession = new Zend_Session_Namespace(‘Main’, true);
Zend_Registry::set(‘defSession’, $defSession);

public static function setupTranslation(){
$options = array(
‘scan’ => Zend_Translate::LOCALE_FILENAME,
‘disableNotices’ => true,
$translate = new Zend_Translate(‘gettext’, Zend_Registry::get(‘siteRootDir’) . ‘/application/languages/’, ‘auto’, $options);
Zend_Registry::set(‘Zend_Translate’, $translate);

if (self::$frontController) {
self::$frontController->registerPlugin(new GSD_Controller_Plugin_Language());

public static function setupRoutes(){
// define some routes (URLs)
$router = self::$frontController->getRouter();

$config = new Zend_Config_Ini(
self::$root . ‘/application/config/routes.ini’,

$router->addConfig($config, ‘routes’);

public static function setupAcl(){
self::$frontController->registerPlugin(new GSD_Controller_Plugin_Acl());

As you can see basicly i just grouped toghether similar comands and make it all look much more nicer and cleaner. And also i added the autoload function since i really think this is a much faster way then the Zend_Loader option. As for what is run, lets take it one by one:
function run
This is the main function that calls the prepare function and starts up the whole Zend Framework.

function prepare
Calls all the internal methods that setup the framework to start dispatching the request.

function setupEnvironment
Here i added the error reporting options and also i define some constants like configuration type ( development – production )

function setupRegistry()
Start the Zend Registry.

function setupConfiguration
Get the config file ( contains the db conection data etc. ) and save the configuration in Zend_Registry

function setupFrontController
Starts the front controller and setup the modular aproach.

function setupErrorHandler
Registers the Error handler plugins and logger.

function setupController
Here is the place to put your Controll Action Helpers

function setupView
start the Zend_View and MVC structure, add the custom HelperPath

function setupDatabase
start your db connection bassed on the configuration we stored in the registry. Also started the Zend_Db_Table cache system.

function setupSessions
Start you session, i used a custom Session Manager that uses a mysql database as storage.

And that is about it. I skipped some of them because i thought they were pretty obvious.

Hope you will find this usefully, and await your comments.

  • http://codeutopia.net/ Jani Hartikainen

    I think the idea of separating bootstrap into a class something like yours is good, but the code you show doesn’t seem particularily different from just sticking it outside a class. You could group the code by say delimiting by comments etc. and it would be pretty much the same. Not to mention that the class is just extraneous code that makes it look more complex.

    Have you looked at Zym_App? I think that’s a pretty good way to implement this kind of bootstrapping class, which actually ends up doing something else than just wrapping code into methods ( http://www.zym-project.com/docs/reference/zym.app.html )

    I guess yours doe have one advantage over the no-class approach: it’s easier to remove selected behavior by commenting out the method call, instead of commenting out the whole code-block.

  • http://www.gsdesign.ro/ Gabi Solomon

    @Jani Hartikainen

    Yes its true that is doesnt have too much of an advantage over just writing the code, but for me its a little more organized and it has one more advantage. I can write a new prepare method for crons that will not start all of the zend framework ( for example it doesnt need the fron controller ) and i can do it in the same file and not have to do another bootstrapfile.

  • http://www.registrycleanersdownload.net/ registry cleaner

    very pleasure to visit this site. nice post. thanks for sharing.

  • http://www.reg-cleaner.org Best reg cleaner

    Oh source code make me dizzyling, but thanks anyway.

  • http://www.best-registrycleaner.net registrycleaner

    Great work! Offers registry cleaners for fix registry errors in windows operating system.

  • http://www.best-registrycleaner.net Best Registry Cleaner

    It's easier to remove selected behavior by commenting out the method call, instead of commenting out the whole code-block.

  • http://www.modularhomesnetwork.com Manufactured Home

    Great website…and cool article. thanks for the post.

  • Pingback: Cron jobs in Zend Framework « DBGLORY 4 YOU

  • leo

    what a mess , php codecs required