Zend Framework's controller uses the frontcontroller design pattern. All requests to the frontcontroller will get rooted through a single entry point, the index.php file.
The index.php will create an instance of zend application and run the application bootstrap process by using the bootstrap.php that we will create in our application folder.
First we define an APPLICATION_PATH, we will use it on several files in our application. Then we define the UPLOAD_PATH which will be used in our application everywhere we need the path of the uploads folder.
Now we define an APPLICATION_ENVIRONMENT. We try to use the value we have set in our htaccess file. If for any reason it's still not set we define production as our default environment type...
Next we add the path to the library directory to the php include path.
Then we can load the Zend Framework Application.php file and several other classes like the cache class. We will need those classes read the config file, cache it, save the it in our cache directory to improve performance, store it in our registry to retrieve it later in your code and finally pass it to the application class constructor to create an instance.
1) We set some variables like the path to cache folder or the configuration file.
2) We check if the cache directory exists, if not we create it.
3) We setup the cache backend and frontend options for our cache instance. We will use the frontend adapter file aswell as the backend adapter file, because with that type of cache we can change values in our configuration file (the application.ini file) and next time we run our application the cache will notice that our configuration file changed and will reload it.
4) Then we check if the configuration is in the cache, if it is we will use the cached configuration. If its not cached (because the cached version expired or its first time we run the application) we will load the configuration and put it into the cache. We pass the environment ass parameter to zend config ini, to load the configuration values coresponding to the environment that is set right now. We will have different configurations for development, testing and production mode.
4) Now we put the configuration in the registry to be able to reuse it anywhere else in our application without having to load it again.
5) Now we are ready run the application. We create an instance of zend application by passing the environment type and our configuration values to the application constructor.
6) We will use try catch, to catch exceptions if bootstrapping fails. If an exception is catched we check what environment type is active, in production mode we only output the error message. If we are not in production mode it means we are in development or testing mode and we also output the stactrace of the error to help us understand the error and therefore debug the application.
Note we will not use the php closing tag at the end of our php files, Zend recommends that for PHP scripts the closing ?> can be omitted.
<?php // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); define('UPLOAD_PATH', realpath(dirname(__FILE__).'/upload')); // Define application environment defined('APPLICATION_ENVIRONMENT') || define('APPLICATION_ENVIRONMENT', (getenv('APPLICATION_ENVIRONMENT') ? getenv('APPLICATION_ENVIRONMENT') : 'production')); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), get_include_path(), ))); // create a autoloader instance require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); // put autoloader instance in registry to retrieve it in bootstrap Zend_Registry::set('Autoloader', $autoloader); // define some values needed by the cache $cacheDirectory = APPLICATION_PATH.'/caches/'; $configurationPath = APPLICATION_PATH.'/configs/application.ini'; $configurationName = 'applicationconfiguration'; $cacheLifetime = 2678400; // check if the cache directory exists, if not create it if (!is_dir($cacheDirectory)) mkdir($cacheDirectory, 0755); // if no lifetime is defined default will be 3600 $frontendOptions = array( 'master_files' => array($configurationPath), 'automatic_serialization' => true, 'lifetime' => $cacheLifetime); $backendOptions = array('cache_dir' => $cacheDirectory); $configurationCache = Zend_Cache::factory('File', 'File', $frontendOptions, $backendOptions); if (!$configuration = $configurationCache->load($configurationName)) { $configuration = new Zend_Config_Ini($configurationPath, APPLICATION_ENVIRONMENT); $configurationCache->save($configuration, $configurationName); } // store configuration in registry Zend_Registry::set('Configuration', $configuration); // create an application instance $application = new Zend_Application( APPLICATION_ENVIRONMENT, $configuration); // BOOTSTRAP try { $application->bootstrap() ->run(); } catch (Exception $exception) { echo '<html><body><center>' . 'An exception occured while bootstrapping the application.'; print_r($exception->getMessage()); if (defined('APPLICATION_ENVIRONMENT') && APPLICATION_ENVIRONMENT != 'production' ) { echo '' . $exception->getMessage() . '' . '<div align="left">Stack Trace:' . '<p>' . $exception->getTraceAsString() . '</p></div>'; } echo '</center></body></html>'; exit(1); }