Deployment environment in zend application



First development goal for any application is to be able to be deployed in different environments and work with minimal reconfiguration. Zend Framework itself has few specific requirements, so application portability is largely a matter of careful design. Most of the differences between one implementation of an application and another occur during initialization where the configuration settings are read in. In a Zend Framework application, this differentiation most commonly occurs in the bootstrap file and application.ini. We can pass the deployment environment as a parameter from the index.php file in the web root as below:

// 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') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(dirname(__FILE__) . './library'),
    realpath(APPLICATION_PATH . './../library'),
    get_include_path(),
)));

/** 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()
            ->run();

application.ini contain most of the configuration details of an application. Path to library, modules, layout and controller and view scripts and the most important database connection parameters.

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
includePaths.library = APPLICATION_PATH "/library"
includePaths.helpers = APPLICATION_PATH "/views/helpers"

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH"/modules"
resources.frontController.params.prefixDefaultModule = false
resources.frontController.params.displayExceptions = 1
resources.modules[] = 

; Layout 
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

resources.view[] =

; database connection
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "db1"

Bootstrap file in application is a child class of Zend_Application_Bootstrap_Bootstrap. We can set all the details of application deployment in bootstrap file also like application.ini

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
     protected function _initView()
    {
        // Initialize view
        $view = new Zend_View();
        $view->doctype('XHTML1_TRANSITIONAL');
	$view->setEncoding('UTF-8');
        $view->headTitle('Application Title');
 
        // 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;
    }
    
    // to set routes of an application
    protected function _initRoutes()
    {
          return $router;
    }
}

By this way we can set an application deployment environment in zend framework.

Hope this will help!