Home >Backend Development >PHP Tutorial >thinkPHP5.0 framework independent configuration and dynamic configuration methods
This article mainly introduces the independent configuration and dynamic configuration methods of thinkPHP5.0 framework. It analyzes the functions, implementation skills and related precautions of independent configuration and static configuration of thinkPHP5.0 framework in the form of examples. Friends in need can refer to the following
The examples in this article describe the independent configuration and dynamic configuration methods of thinkPHP5.0 framework. Share it with everyone for your reference, the details are as follows:
Independent configuration file:
The new version supports configuration file separation, you only need to configure the extra_config_list
parameter ( in the application public configuration file).
For example, if you do not use a separate configuration file, the database configuration information should be configured in config.php as follows:
/* 数据库设置 */ 'database' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', // 数据库调试模式 'debug' => false, ],
If you need to use an independent configuration file, first add the configuration in config.php:
'extra_config_list' => ['database'],
After defining, the database You can use the database.php file independently for configuration. The configuration content is as follows:
/* 数据库设置 */ return [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'thinkphp', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', // 数据库调试模式 'debug' => false, ],
If the extra_config_list parameter is configured, and at the same time in config. If configured in both php and database.php files, the configuration in the database.php file will override the settings in config.php.
The parameter acquisition of independent configuration files is a two-dimensional configuration method. For example, to obtain the type parameter of the database independent configuration file, it should be:
Config::get('database.type');
To obtain the parameters of the complete independent configuration file, use:
Config::get('database');
System default settings Two independent configuration files are created, including database and validate, which are used to set database configuration and validation rule definitions respectively.
Dynamic configuration:
Set configuration parameters
Use the set method to dynamically set parameters, for example:
Config::set('配置参数','配置值'); // 或者使用助手函数 config('配置参数','配置值');
can also be set in batches, for example:
Config::set([ '配置参数1'=>'配置值', '配置参数2'=>'配置值' ]); // 或者使用助手函数 config([ '配置参数1'=>'配置值', '配置参数2'=>'配置值' ]);
Related recommendations:
How to make thinkphp5 load static resource paths and constants
thinkPHP framework automatic filling principle and usage analysis
The above is the detailed content of thinkPHP5.0 framework independent configuration and dynamic configuration methods. For more information, please follow other related articles on the PHP Chinese website!