Example Analysis of Dynamic Configuration Usage of thinkPHP Framework

  • 2021-10-16 01:14:04
  • OfStack

This article illustrates the dynamic configuration usage of thinkPHP framework. Share it for your reference, as follows:

Recently, when using @ ThinkPHP as a system, one function should be used, and the configuration parameters of the system should be dynamically saved to Config file. In the past, when we made the system, the configuration parameters of the project were directly written into the Config/Config. php file in advance, and then applied in the project. However, in some projects, users need to configure parameters according to their own situation, and set them dynamically in the background. There are two ways to configure this dynamic parameter, one is to write to the database, and the other is to write to the configuration file. Today, I will talk about implementing this function in the form of configuration file.

1. Profile settings

First, we create a new configuration file named setting. config. php under the TP project configuration directory Config, which is used to save dynamic parameters. Then, setting. config. php is merged in the project master configuration file Config. php by merging arrays. In this way, the configuration parameters in setting. config. php can be invoked throughout the project.

2. Implement dynamic management parameters

In the background, establish a function to read out the default values of setting. config. php and display them in a form. This can be achieved using the C function of TP. Then, you can set the values of each parameter in the form. After the form is submitted and saved, the value submitted by the form is processed. The specific code is as follows:

Structure of setting. config. php file


<?php
return array(
  'setting'=>array(
    'tel' => '400-088-7380',
        'qq'  => '88888888',
        ......
    ),
);
?>

Operation to save configuration parameters


function SaveSetting(){
//setting.config.php The path of the file, through the settingfile_path To set; 
$setfile='./Home'.C('settingfile_path');
$a=C('setting'); // Assigns the contents of the default configuration parameter to the $a;
$b=array(
  'tel' => $_POST['tel'],
  'web' => $_POST['web'],
  ........
);
// Here, the new parameter values are submitted through the background form; 
$c=array_merge($a,$b) ;

Merge the arrays $a and $b; We know, array_merge() Function, you can merge two arrays, and if the array elements have the same key name, the following values overwrite the previous values (except numeric key names);

Then, after traversing the value of the merged array $c, the php file code is generated;


$settingstr="<?php \n return array(\n'Setting' =>array(\n";
foreach($c as $key=>$v){
  $settingstr.= "\t'".$key."'=>'".$v."',\n";
}
$settingstr.="),\n);\n?>\n";
file_put_contents($setfile,$settingstr); // Pass file_put_contents Save setting.config.php Documents; 

At this point, the values of the configuration parameters for the setting. config. php file have been updated;

Writing to a file is an array, but you can refer to 1 var_export Function, there is no need to loop the array!


$settingstr="<?php \n return array(\n'Setting' =>array(\n";
 foreach($c as $key=>$v){
  $settingstr.= "\t'".$key."'=>'".$v."',\n";
 }
$settingstr.="),\n);\n?>\n";
file_put_contents($setfile,$settingstr); // Pass file_put_contents Save setting.config.php Documents ;

The above code can be modified to:


$settingstr = "<?php \n return array(\n'Setting' =>\n".var_export($c,true)." \n ?>";
file_put_contents($setfile,$settingstr); // Pass file_put_contents Save 

(Thinkphp version 3.1 already supports the C function to save the set parameter values, so this method is suitable for TP version 3.0 and below)

In addition, we know that TP will generate an runtime cache file from all the configuration files and configuration parameters of the project during the first run. If we update the contents of congfig, the cache file in the project must be deleted before it can take effect. For this reason, we let the system empty the cache and update the parameters by itself. The code is as follows:


//RUNTIME_FILE Constants are configured in the entry file runtimefile The path and file name of; 
if(file_exists(RUNTIME_FILE)){
  unlink(RUNTIME_FILE); // Delete RUNTIME_FILE;
}

It is not enough to delete runtime_file, but to empty the files in Cache folder under 1; The code is as follows:


$cachedir=RUNTIME_PATH."/Cache/";  //Cache The path of the file; 
if ($dh = opendir($cachedir)) {   // Open Cache Folder; 
  while (($file = readdir($dh)) !== false) {  // Traversal Cache Directory, 
       unlink($cachedir.$file);        // Delete every traversed 1 Files; 
  }
  closedir($dh);
}

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: