Bootstrapping the Application: the index.php File
Saturday, September 19, 2009
The index.php file can be divided into three sections:
- Defining the global constants
- Setting the include paths
- Creating and running the Zend_Application object
I would recommend defining two global constants:
- APPLICATION_PATH: This is the location of the application in the file system; it should be determined dynamically based on the location of the index.php file. This makes it easy to copy the application between environments where its location in the file system will be different.
- APPLICATION_ENV: This is used whether the application is running live, or is in some sort of development or testing environment. The possible values I use on the GM-RAM website are: development, testing, staging, production.
Here is the code for setting the constants:
// Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Define application environment defined('APPLICATION_ENV') || define('APPLICATION_ENV', 'development'); // development, testing, staging, production
Next, you need to make sure that the application can find the Zend Framework. If the framework is installed on the server and the include path already points to it, you don’t need to add any code. However, if it is necessary to install your own copy of the framework, you will also need to set the include path:
// Add the Zend Framework directory to the include_path set_include_path(implode(PATH_SEPARATOR, array( dirname(dirname(__FILE__)) . '/library/ZendFramework-1.8.4PL1', get_include_path(), )));
Although it is possible to place the Zend Framework files directly into the library directory, this can make it awkward to update the framework when the site is live. I prefer to add another level to the directory structure; under library I create a directory with a name reflecting the version of the framework (e.g. ZendFramework-1.8.4PL1), and place the framework files inside that. This allows me to upgrade via a simple two-step process:
- Create a directory for the new version of the framework (e.g. ZendFramework-1.9.0) in the library directory, and copy the new framework files into it.
- Change the name of the directory added to the include path from /library/ZendFramework-1.8.4PL1 to /library/ZendFramework-1.9.0.
Finally, you need to create and run the Zend_Application object:
/** 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();
The two parameters passed to the constructor specify:
- The application environment
- The configuration file
The application environment is used to indicate which parts of the configuration file should be used.
I shall start looking at the configuration file in my next post.


