Recently, when using @ ThinkPHP as a system, to use a function, we need to dynamically save the system configuration parameters to the Config file. in the past, when we were doing the system, configuration parameters of the project are directly written to Config/Config. php file, and then in the project...
Recently, when using @ ThinkPHP as a system, to use a function, we need to dynamically save the system configuration parameters to the Config file. in the past, when we were doing the system, configuration parameters of the project are directly written to Config/Config. in the php file, and then apply it in the project. However, for some projects, you need to configure parameters dynamically based on your own situation, generally, we have two methods: writing data to the database and writing data to the configuration file. today, I am talking about using the configuration file to implement this function.
1. configuration file settings
First, create a configuration file named setting under the Config Directory of the TP project. config. php, this file is used to save dynamic parameters, and then the main configuration file Config. in php, the setting. config. php is merged, so that setting can be called throughout the project. config. the configuration parameters in php are.
2. implementing dynamic management parameters
Create a function in the background to set setting. config. the default value of php is read and displayed in a form. you can use the C function of TP to implement it. then, you can set the values of each parameter in the form. after the form is submitted and saved, process the value submitted by the form. the code is as follows.
Setting. config. php file structure
-
- Return array (
- 'Setting' => array (
- 'Tel '=> '2017-088-7380 ',
- 'Web' => 'Www .sinra.cn ',
- 'QQ' => '123 ',
- ......
- ),
- );
- ?>
- // Save the configuration parameters
- Function SaveSetting (){
- // Setting. config. php file path, which can be set through settingfile_path;
- $ Setfile = './home'. C ('settingfile _ path ');
- $ A = C ('setting'); // assign the content of the default configuration parameter to $;
- $ B = array (
- 'Tel '=> $ _ POST ['Tel'],
- 'Web' => $ _ POST ['web'],
- ........
- );
- // Submit the new parameter value in the background form;
- $ C = array_merge ($ a, $ B); // merges arrays $ a and $ B. We know that the array_merge () function can merge two arrays, in addition, if the array element has the same key name, the subsequent value will overwrite the previous value (except the number key name );
Then, the merged array $ c is traversed and the PHP file code is generated:
- $ Settingstr =" Array (\ n ";
- Foreach ($ c as $ key => $ v ){
- $ Settingstr. = "\ t'". $ key. "'=>'". $ v. "', \ n ";
- }
- $ Settingstr. = "), \ n); \ n?> \ N ";
- File_put_contents ($ setfile, $ settingstr); // Save the setting. config. php file through file_put_contents;
At this point, the value of the configuration parameter in the setting. config. php file has been updated;
(Thinkphp3.1 version already supports C function to save the set parameter values. Therefore, this method is suitable for TP of version 3.0 and earlier)
In addition, we know that TP will generate a runtime cache file for all configuration files and configuration parameters of the project during the first run. if we update the content of congfig, you must delete the cache file in the project before it takes effect. Therefore, let the system clear the cache and update the parameters. the code is as follows:
- // The constant RUNTIME_FILE is the path and file name of the runtimefile configured in the entry file;
- If (file_exists (RUNTIME_FILE )){
- Unlink (RUNTIME_FILE); // delete RUNTIME_FILE;
- }
- It is not enough to delete runtime_file. you need to clear the files in the Cache folder. the code is as follows:
- $ Cachedir = RUNTIME_PATH. "/Cache/"; // Cache file path;
- If ($ dh = opendir ($ cachedir) {// open the Cache folder;
- While ($ file = readdir ($ dh ))! = False) {// traverse the Cache Directory,
- Unlink ($ cachedir. $ file); // delete each file that is traversed;
- }
- Closedir ($ dh );
- }
At this point, the entire dynamic configuration parameter file and the Cache clearing function are all implemented. the above code is passed in the TP3.0 framework.