Hey folks, just wanted to get some input on my newly modified bootstrap file. I have to switch between production and development quite frequently as the updates go up fairly fast. So I modified the Zend Registry Database section for use in my application where I use Zend_Auth adapters. That way instead of setting the db information in the bootstrap as well as config I now just use the config file and depending on what the environment is is what the db will turn to. I added the getroot function since some of my production servers not hosted by me has their Doc_root set without a / at the end and some other do have it. So to solve some problems I just added the very basic function to check for / at the end. Would love some feedback as to whether something is insecure etc. This may also help some newer members to get their setups going where they couldn't figure out how to set additional features in the bootstrap for instance dojo / database / timezone etc.
[PHP]<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
/**
* Bootstrap autoloader for application resources
*
* @return Zend_Application_Module_Autoloader
*/
public static $root = '';
public static $registry;
public static $db;
public function getRoot(){
$path = $_SERVER['DOCUMENT_ROOT'];
if (substr($path,strlen($path) -1,strlen($path)) == '/'){
$path = substr($path,0,strlen($path)-1);
}
return $path;
}
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader ( array ('namespace' => 'Default', 'basePath' => dirname ( __FILE__ ) ) );
Zend_Session::setOptions(array('remember_me_second s' => 7200));
Zend_Session::start();
self::$root = $this->getRoot();
self::setConfig();
self::setRegistry();
self::setTimeZone();
self::setupDatabase();
return $autoloader;
}
//Date time setting will be done here
public static function setTimeZone()
{
date_default_timezone_set('Europe/London');
}
//Configuration file reading & setting up configuration will be done using following.
public static function setConfig(){
$config = new Zend_Config_Ini(
self::$root . '/../application/configs/application.ini',APPLICATION_ENV);
self::$registry->configuration = $config;
self::$db = self::$registry->configuration->resources->toArray();
}
public static function setRegistry()
{
self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
Zend_Registry::setInstance(self::$registry);
}
/*DB setup done here.
Read configuration, create db instance & set in registry to use in other part of application.*/
public static function setupDatabase()
{
$config = self::$db;
$db = Zend_Db::factory($config["db"]["adapter"], $config["db"]["params"]);
$db->query("SET NAMES 'utf8'");
self::$registry->database = $db;
Zend_Db_Table::setDefaultAdapter($db);
}
protected function _initView() {
// Initialize view
$view = new Zend_View ( );
//$view->doctype('XHTML1_STRICT'); /* Quick Change out depending on project */
$view->doctype ( 'XHTML1_TRANSITIONAL' ); /* Quick Change out depending on project */
$view->env = APPLICATION_ENV;
Zend_Dojo::enableView ( $view );
Zend_Dojo_View_Helper_Dojo::setUseDeclarative ();
$view->dojo ()->setLocalPath ( "/js/prod/dojo/dojo.js" )
->setDjConfigOption ( 'usePlainJson', true )
->setDjConfigOption ( 'parseOnLoad', true )
->addStylesheetModule ( 'dijit.themes.soria' )
->addStylesheet ( '/js/prod/dojox/grid/resources/soriaGrid.css' )
->enable ();
// Add it to the ViewRenderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelp er ( 'ViewRenderer' );
$viewRenderer->setView ( $view );
// Return it, so that it can be stored by the bootstrap
return $view;
}
}
[/PHP]
And the actual application.ini file is below:
Code:
[production]
# Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
# Include path
includePaths.library = APPLICATION_PATH "/../library"
# Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
# Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
# Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
# Views
resources.view[] =
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
# Database # REMEMBER TO SET VALUES IN BOOTSTRAP ON CHANGE
resources.db.adapter = PDO_MYSQL
resources.db.params.host = prodserver.somedomain
resources.db.params.username = produser
resources.db.params.password = prodpass
resources.db.params.dbname = proddb
resources.db.isDefaultTableAdapter = true
# Session
resources.session.remember_me_seconds = 864000
[testing : production]
# Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
resources.db.params.host = localhost
resources.db.params.username = testuser
resources.db.params.password = testpass
resources.db.params.dbname = testdb
# Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1