Usage analysis on dynamic configuration of thinkphp frame

Source: Internet
Author: User
This article mainly introduced the thinkphp framework dynamic configuration usage, combined with the instance form analysis thinkPHP3.0 before the dynamic configuration operation skill and the cache related operation notice, the need friend can refer to the next

This article describes the thinkphp framework dynamic configuration usage. Share to everyone for your reference, as follows:

Recently when using @thinkphp to do the system, to use a function, it is necessary to dynamically save the system configuration parameters to the config file. In the past, when we made the system, the configuration parameters of the project were written directly to the config/config.php file and then applied in the project. However, some projects, users need to according to their own situation will be configured parameters, through the background dynamic to set. This dynamic parameter configuration, generally we have two ways, one is to write to the database, and the other is to write to the configuration file. Today, let me say that this is accomplished in the form of a configuration file.

One, configuration file settings

First, we create a new configuration file under the TP project configuration directory config, named setting.config.php, which is used to save dynamic parameters. The setting.config.php is then merged into the project master configuration file config.php in the form of a merged array. In this way, the configuration parameters in setting.config.php can be invoked throughout the project.

Second, the realization of dynamic management parameters

In the background, a feature is created that reads the default values of the setting.config.php and displays them in a single form. This can be achieved using the C function of TP. You can then set the values for each parameter in the form. After the form submission is saved, the value of the form submission is processed, with the following code:

Structure of the setting.config.php file

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

Actions to save configuration parameters

function SaveSetting () {//setting.config.php file path, set by Settingfile_path; $setfile = './home '. C (' Settingfile_path '); $a =c (' setting '); Assign the contents of the default configuration parameter to $ 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 array $ A and $b; we know that array_merge() functions can merge two arrays, and if the array element has the same key name, the following value overrides the previous value (except the numeric key name);

Then, after iterating through the values 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); Save setting.config.php files through file_put_contents;

To this, the value of the configuration parameter of thesetting.config.php file has been updated;

Write the file is the number of groups is not false, but you can refer to the 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); Save setting.config.php files through file_put_contents;

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); Save with File_put_contents

(Thinkphp3.1 version, the C function has been supported to save the parameter values of the settings, so, this method, suitable for 3.0 and the following version of TP)

In addition, we know that TP in the first run will be the project all the configuration files, configuration parameters, etc., generate a runtime cache file, if we update the content of Congfig, the project must be deleted before the cache file to take effect . To do this, we let the system itself to empty the cache and update the parameters. The code is as follows:

The Runtime_file constant is the path and file name of the runtimefile configured in the portal file; if (file_exists (Runtime_file)) {  unlink (runtime_file);// Delete Runtime_file;}

Light Delete Runtime_file is not enough, to clear the cache folder files, the code is as follows:

$cachedir =runtime_path. " /cache/";  The path to the cache file, 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);}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.