Zend Framework Bootstrap file class
by Gabi SolomonAfter 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';
-
-
Bootstrap::run();
Pretty simple isnt it
. And now the init file.
-
// Add /library and /application directory to our include path
-
-
-
APPLICATION_PATH . '/models' . PATH_SEPARATOR
-
. APPLICATION_PATH . '/modules/default/controllers' . PATH_SEPARATOR
-
. APPLICATION_PATH . '/controllers' . PATH_SEPARATOR
-
. APPLICATION_PATH . PATH_SEPARATOR
-
. $siteRootDir . '/library' . PATH_SEPARATOR
-
);
-
-
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) {
-
}
-
-
class Bootstrap{
-
-
-
-
-
self::prepare();
-
-
$response = self::$frontController->dispatch();
-
self::sendResponse($response);
-
}
-
-
self::setupEnvironment();
-
-
self::setupRegistry();
-
self::setupConfiguration();
-
-
self::setupFrontController();
-
self::setupErrorHandler();
-
-
self::setupController();
-
self::setupView();
-
self::setupDatabase();
-
self::setupSessions();
-
self::setupTranslation();
-
self::setupRoutes();
-
self::setupAcl();
-
}
-
-
-
date_default_timezone_set('Europe/Bucharest');
-
-
-
$configType = (isset($_SERVER['SERVER_NAME']) && ($_SERVER['SERVER_NAME'] == '127.0.0.1' OR $_SERVER['SERVER_NAME'] == 'localhost')) ? 'development' : 'production';
-
-
}
-
-
Zend_Registry::setInstance(self::$registry);
-
}
-
-
-
$config = new Zend_Config_Ini(
-
self::$root . '/application/config/main.ini',
-
APPLICATION_ENVIRONMENT
-
);
-
-
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'] );
-
}
-
-
self::$frontController = Zend_Controller_Front::getInstance();
-
self::$frontController->throwExceptions(true);
-
self::$frontController->returnResponse(true);
-
-
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);
-
self::$frontController->setResponse($response);
-
}
-
-
self::$frontController->throwExceptions(false);
-
-
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);
-
Zend_Registry::set('logger',$logger);
-
}
-
-
// place to put in your Controll Action Helpers
-
// ex: Zend_Controller_Action_HelperBroker::addHelper(new GSD_Controller_Action_Helper_AuthUsers());
-
}
-
-
-
$view->addHelperPath('GSD/View/Helper', 'GSD_View_Helper_');
-
-
$viewRendered = new Zend_Controller_Action_Helper_ViewRenderer($view);
-
Zend_Controller_Action_HelperBroker::addHelper($viewRendered);
-
-
Zend_Layout::startMvc(
-
'layoutPath' => self::$root . '/application/layouts',
-
'layout' => 'default'
-
)
-
);
-
}
-
-
$response->sendResponse();
-
}
-
-
$config = self::$registry->configuration;
-
-
$db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
-
-
$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
-
$profiler->setEnabled(true);
-
$db->setProfiler($profiler);
-
-
$db->query("SET NAMES 'utf8'");
-
-
self::$registry->database = $db;
-
Zend_Db_Table::setDefaultAdapter($db);
-
-
'automatic_serialization' => true
-
);
-
-
'cache_dir' => self::$root . '/data/cache/db_table'
-
);
-
-
$cache = Zend_Cache::factory('Core',
-
'File',
-
$frontendOptions,
-
$backendOptions);
-
-
// Next, set the cache to be used with all table objects
-
Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
-
}
-
-
-
// 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
-
));
-
Zend_Session::setSaveHandler($sessionManager);
-
-
$defSession = new Zend_Session_Namespace('Main', true);
-
Zend_Registry::set('defSession', $defSession);
-
}
-
-
'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());
-
}
-
}
-
-
// define some routes (URLs)
-
$router = self::$frontController->getRouter();
-
-
$config = new Zend_Config_Ini(
-
self::$root . '/application/config/routes.ini',
-
'development'
-
);
-
-
$router->addConfig($config, 'routes');
-
}
-
-
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.
Cheers
Related Posts
-
Jani Hartikainen
-
Gabi Solomon
-
registry cleaner

