Dynamic configuration, extended configuration and batch configuration of ThinkPHP framework configuration


This paper describes the dynamic configuration, extended configuration and batch configuration of ThinkPHP framework configuration operation. For your reference, the details are as follows:

Dynamic configuration


C ('parameter name ',' new parameter value ')

For example, we can use the

//Dynamically changing cache expiration

The dynamic configuration assignment is only valid for the current request and will not affect future requests.

The method of dynamically changing the configuration parameters is very similar to the method of reading the configuration. Both methods use the C method, but the parameters are different.

It can also support the reading and setting of two-dimensional arrays, and operate with point syntax, as follows:

//Gets the parameter value that has been set
//Set a new value

Extended configuration

Extension configuration can support automatic loading of additional custom configuration files, and the configuration format is the same as the project configuration.

The extension configuration can be set as follows (multiple files are separated by commas)

//Load extended profile
'LOAD_EXT_CONFIG' => 'user,db',

Assume extended profileuser.php anddb.phpIt is used for user configuration and database configuration respectively. The advantage of this is that even if debugging mode is turned off in the future, it will still take effect automatically after you modify the DB configuration file.

If it is configured in the application public settings file, the configuration file under the application public configuration directory will be automatically loadedApplication/Common/Conf/user.phpandApplication/Common/Conf/db.php

If it is configured in the configuration file of the module (assuming the home module), the configuration file under the module directory will be automatically loadedApplication/Home/Conf/user.php andApplication/Home/Conf/db.php

By default, the settings parameters in the extended configuration file are incorporated into the project configuration file. That is, by default, they are all level-1 configuration parameters, such as user.php The configuration parameters in are as follows:

//User profile
return array(
  'USER_ Type '= > 2, // user type
  'USER_ AUTH_ ID '= > 10, // user authentication ID
  'USER_ AUTH_ Type '= > 2, // user authentication mode

Then, the final way to obtain user parameters is as follows:


If the configuration file is changed to:

//Load extended profile
'LOAD_EXT_CONFIG' => array('USER'=>'user','DB'=>'db'),

Then the method of obtaining user parameters is changed to:


Batch configuration

The C configuration method supports batch configuration, such as:

$config = array('WEB_ SITE_ TITLE'=>'ThinkPHP','WEB_ SITE_ Description '= >'open source PHP framework');

The configuration parameters in the $config array are merged into the existing global configuration.

You can read the configuration parameters in the database in this way, for example:

//Read the configuration in the database (assuming there is a config table to hold the configuration parameters)
$config =  M('Config')->getField('name,value');
//Config is an associative array. The key value is the configuration parameter value, which is the configuration value
//For example: array ('config1 '= >'val1','config2 '= >'val2',...)
C ($config); // merge configuration parameters to global configuration

After merging, we can read the configuration parameters in the database just as we read the common configuration parameters before. Of course, we can also change them dynamically.

//Read configuration parameters from the database merged into the global configuration
//Dynamically change configuration parameters (the current request is valid and will not be automatically saved to the database)

More about ThinkPHP related content, interested readers can view this site topic: “ThinkPHP introduction tutorial”, “ThinkPHP template operation skills summary”, “ThinkPHP common methods summary”, “CodeIgniter introductory tutorial”, “Ci (CodeIgniter) framework advanced tutorial”, “Zend framework framework introduction tutorial” and “PHP template technology summary”.

I hope this article will be helpful to the PHP program design based on ThinkPHP framework.

Recommended Today

How to use IntelliJ idea quickly

Note that all of the following actions indicate the location of the menu items, and you should assign shortcut keys to these menu items (except for the Efficiency Guide). 1、 See the efficiency guide The efficiency guide lists all the ways to improve your development efficiency. 2、 View recently opened files You can see which […]