Posted: July 22nd, 2009

ZF 1.8 Bootstrap

Category: technical

ok i have been playing with zend at home to increase my knowledge, and i have run across this, i think its pretty handy, and gave me much insight in zend’s 1.8 bootstrapping method. i didnt use any of the dojo stuff, mainly because i plan to use jquery for all my dynamic javascripting needs.

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_seconds’ => 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::getStaticHelper ( ‘ViewRenderer’ );
        $viewRenderer->setView ( $view );
        // Return it, so that it can be stored by the bootstrap
        return $view;
    }
}

more can be read here i give credit to the guy who wrote this up originally and shared with the rest of the world.