Php – How Does the Zend Application / Bootstrapping Work

PHPzend-framework

A few questions regarding the basics of Zend Framework 1.9.

  1. i followed the quickstart guide, and basically, bootstrapping involves,

    a. from index.php:

    $ZEND_FRAMEWORK_LIB_PATH = '/appl/ZendFramework-1.9.7/library';
    defined('APPLICATION_PATH') || define('APPLICATION_PATH', (realpath(dirname(__FILE__) . '/../application')));
    defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
    set_include_path(implode(PATH_SEPARATOR, array((dirname(dirname(__FILE__)) . '/library'), $ZEND_FRAMEWORK_LIB_PATH, get_include_path(),)));
    require_once 'Zend/Application.php';
    $application = new Zend_Application(APPLICATION_ENV, (APPLICATION_PATH . '/configs/application.ini'));
    $application->bootstrap()->run();
    

    b. Then in the Bootstrap.php, i have

    protected function _initAutoload()
    {
        $autoloader = new Zend_Application_Module_Autoloader(array("namespace" => "Default_", "basePath" => dirname(__FILE__),));
        return $autoloader;
    }
    
    protected function _initDoctype()
    {
        $this->bootstrap("view");
        $view = $this->getResource("view");
        $view->doctype("XHTML1_STRICT");
    }
    
  2. For a start, a few things i don't understand:

    a. If a user access the site not via the default index.php, does that mean that bootstrapping (and indeed, all the code in the index.php including setting of environment etc, will be bypassed?)

    b. There is no place that explicitly calls the Bootstrap's _initAutoload() or _initDoctype() methods. So when are these methods implicitly invoked?

    c. Since in the index.php, i have already "passed in" the config file '/configs/application.ini' to the Zend_Application constructor, is there any way to retrieve the config entries elsewhere?

  3. In my application, i have to work with different databases (so i can't just use resources.db.*). So in the same application.ini file, i have, e.g.

    custdb.adapter = "PDO_MYSQL"
    custdb.params.host = "localhost"
    custdb.params.username = "username"
    custdb.params.password = "password"
    custdb.params.dbname = "custdb"
    

    What's the best practice to manage the DB adapter?

    a. Is it possible to (and should i) create the DB adapter in index.php OR Bootstrap.php and retrieve it elsewhere when needed (and how)?

    b. Or is possible to (and should i) just retrieve the config entries elsewhere (how?) and instantiate the DB adapter as and when needed?

Thanks!

Best Answer

Here's a few answers.

2a. All request are redirected to index.php. This is done with mod_rewrite and specified in the .htaccess file.

2b. The bootstrap calls any method prefixed with _init. See Zend Framework - Theory of Operation

2c. Yes. Zend::Config. You could store an instance in Zend::Registry for easy access. Eg:

$config = new Zend_Config((APPLICATION_PATH . '/configs/application.ini')); 
$application = new Zend_Application(APPLICATION_ENV, $config);
Zend_Registry::set('config', $config);

Check the API reference to see the constructors for these two classes.

I don't think the Quick start is that helpful. I'd recommend a getting a book. I enjoyed "Zend Framework 1.8 Web Application Development" by Keith Pope.

Related Topic