+ Reply to Thread
Results 1 to 4 of 4

Thread: Modified ZF 1.8 Bootstrap

  1. #1
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default Modified ZF 1.8 Bootstrap

    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

  2. #2
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default Index.php

    Just figured I would add my index.php file as well for you to review.

    It uses the default zf 1.8 directory structure. So changing between prod / development is done by just changing the application env in index.php from : development to : production.
    [PHP]<?php
    // Set the initial include_path. You may need to change this to ensure that
    // Zend Framework is in the include_path; additionally, for performance
    // reasons, it's best to move this to your web server configuration or php.ini
    // for production.
    set_include_path(implode(PATH_SEPARATOR, array(
    realpath(dirname(__FILE__) . '/../library'),
    get_include_path(),
    )));

    // Define path to application directory
    defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

    // Define application environment
    defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));

    /** Zend_Application */
    require_once 'Zend/Application.php';

    // Create application, bootstrap, and run
    $application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap();
    $application->run();[/PHP]

  3. #3
    DuGi is offline Junior Member
    Join Date
    Jun 2009
    Posts
    1

    Default

    Very nice post. Helped me a lot. Especially the part with the bootstrap setup

    Acutally I found your post, because I was searching for a little help and the Zend_Auth matter.

    I am trying to do precise the thing you write you're doing - making the DB as a defaultAdapter. I can't seem to get that part working with Zend_Auth

    So I was wondering If you maybe could post your code or some examples of it?

    Thank you in advance

    Have a nice day.

  4. #4
    dele454's Avatar
    dele454 is offline Member
    Join Date
    Jun 2008
    Posts
    49

    Default Thanks

    Thanks for sharing this code with the community. The last time i worked on setting up a bootstrap file was when ZF was of ver1.6. Now it is ver1.9 and alot has changed and i am having to learn some new things to get to speed.

    You code really got me started.I tried following the ones on the ZF website but wasnt running at all.

    Thanks mate

+ Reply to Thread

Similar Threads

  1. bootstrap zend_cache
    By wlp1979 in forum Core Infrastructure
    Replies: 3
    Last Post: 11-11-2009, 10:14 PM
  2. Redirect in the Bootstrap.php?
    By stconrad in forum Authentication & Authorization
    Replies: 1
    Last Post: 09-16-2009, 09:15 AM
  3. Getting module name in bootstrap
    By Juul in forum General Q&A on Zend Framework
    Replies: 0
    Last Post: 09-15-2008, 04:40 PM
  4. How do I set the Doctype in the Bootstrap?
    By notrub225 in forum General Q&A on Zend Framework
    Replies: 4
    Last Post: 04-29-2008, 04:55 PM
  5. Bootstrap in IIS
    By spectravp in forum Model-View-Controller (MVC)
    Replies: 0
    Last Post: 03-15-2008, 04:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts