欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

The index.php file (public / index.php)

程序员文章站 2022-06-02 09:10:46
...
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.

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 '

' . 'An exception occured while bootstrapping the application.'; print_r($exception->getMessage()); if (defined('APPLICATION_ENVIRONMENT') && APPLICATION_ENVIRONMENT != 'production' ) { echo '' . $exception->getMessage() . '' . '
Stack Trace:' . '

' . $exception->getTraceAsString() . '

'; } echo '
'; exit(1); }