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

Time:2020-10-24

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

Format:

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

For example, we can use the

//Dynamically changing cache expiration
C('DATA_CACHE_TIME',60);

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
C('USER_CONFIG.USER_TYPE');
//Set a new value
C('USER_CONFIG.USER_TYPE',1);

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:

<?php
//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:


C('USER_AUTH_ID');

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:


C('USER.USER_AUTH_ID');

Batch configuration

The C configuration method supports batch configuration, such as:

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

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
C('CONFIG1');
//Dynamically change configuration parameters (the current request is valid and will not be automatically saved to the database)
C('CONFIG2','VALUE_NEW');

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.