The application configuration determines how the Yii application should be executed, because this is the only parameter passed through the entry. An application has different execution methods in different modes. For example, in the production mode,
The application configuration determines how the Yii application should be executed, because this is the only parameter passed through the entry. An application has different execution methods in different modes. For example, different configurations are required for applications in the production, development, and test modes. In team development, each developer should have their own database connection and custom application configuration. In this tutorial, we describe how to manage application configurations to meet all of the above requirements.
Before starting, we should pay attention to the application configuration to be saved as a php script. Therefore, we can place anyValid php codeIn it, this makes the configuration more 'smart' than simply putting back the value pair (name-value pairs ).
First, createMain)Configure the application and save itMain. phpFile. This file should contain all the necessary configurations in production mode.
Next we createDevelopment)Configure the application and save itDev. php. Because the development configuration is mostly the same as the master configurationInheritance. We use CMap: mergeArray to implement inheritance.
array( 'db'=>array( // define DB connection used for development ), ), ) );
The code first contains the main. php file and a custom configuration array (the example shows the database linkDB connection). Then, the result of merging the two configurations is returned as the final development configuration. Note that we didn't use the php function array_merge () or array_merge_recursive () here because they won't merge the two arrays as we expected.
We can define it in the same way.TestConfigure the application and save it as test. php.
In order to run programs in different modes (production, development, or testing), we should use the corresponding configuration at the entrance. To avoid the trouble of modifying the entry file when switching mode, we can create an independent entry for each mode. For example we can create index. php, index-dev.php and index-test.php correspond to production, development, and test modes respectively. In production mode we access index. php through a browser, access index_dev.php in development mode, and access index-test.php in test mode.
In the development environment of the team, the source code control system (such as SVN, CVS, and GIT) is used for control. every developer wants to have independent application configurations (such as they have different database connections ). Therefore, in the source code library, we should only saveMain. phpFile. The remaining part of the configuration file of each developer should be kept locally to avoid conflicts.
Tip: The same method can be used with other PHP-based configurations. For example, we save the application parameters (accessed by Yii: app ()-> params) as a PHP file, we can use the above method to customize different parameters in different modes
This article translated from foreign language website, view the original, please click: http://www.yiiframework.com/wiki/32/manage-application-configuration-in-different-modes/