ThinkPHP implements dynamic config file configuration and dynamic Cache clearing

Source: Internet
Author: User
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

  1. Return array (
  2. 'Setting' => array (
  3. 'Tel '=> '2017-088-7380 ',
  4. 'Web' => 'Www .sinra.cn ',
  5. 'QQ' => '123 ',
  6. ......
  7. ),
  8. );
  9. ?>
  10. // Save the configuration parameters
  11. Function SaveSetting (){
  12. // Setting. config. php file path, which can be set through settingfile_path;
  13. $ Setfile = './home'. C ('settingfile _ path ');
  14. $ A = C ('setting'); // assign the content of the default configuration parameter to $;
  15. $ B = array (
  16. 'Tel '=> $ _ POST ['Tel'],
  17. 'Web' => $ _ POST ['web'],
  18. ........
  19. );
  20. // Submit the new parameter value in the background form;
  21. $ 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:

  1. $ Settingstr =" Array (\ n ";
  2. Foreach ($ c as $ key => $ v ){
  3. $ Settingstr. = "\ t'". $ key. "'=>'". $ v. "', \ n ";
  4. }
  5. $ Settingstr. = "), \ n); \ n?> \ N ";
  6. 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:

  1. // The constant RUNTIME_FILE is the path and file name of the runtimefile configured in the entry file;
  2. If (file_exists (RUNTIME_FILE )){
  3. Unlink (RUNTIME_FILE); // delete RUNTIME_FILE;
  4. }
  5. It is not enough to delete runtime_file. you need to clear the files in the Cache folder. the code is as follows:
  6. $ Cachedir = RUNTIME_PATH. "/Cache/"; // Cache file path;
  7. If ($ dh = opendir ($ cachedir) {// open the Cache folder;
  8. While ($ file = readdir ($ dh ))! = False) {// traverse the Cache Directory,
  9. Unlink ($ cachedir. $ file); // delete each file that is traversed;
  10. }
  11. Closedir ($ dh );
  12. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.