Home > Article > Backend Development > About independent configuration and dynamic configuration methods in thinkPHP5.0 framework
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 necessary For an independent configuration file, first add the configuration in config.php:
'extra_config_list' => ['database'],
After definition, the database configuration can be used independently using the database.php file to configure the content 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 configured in both the config.php and database.php files, the configuration of the database.php file Will overwrite the settings in config.php.
The parameters of the independent configuration file are obtained in two-dimensional configuration. 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');
The system has set up two independent configuration files by default, including database and validate. Used to set up database configuration and validation rule definition 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'=>'配置值' ]);
The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Thinkphp configuration file path method
##ThinkPHP cache fast cache and dynamic cache
The above is the detailed content of About independent configuration and dynamic configuration methods in thinkPHP5.0 framework. For more information, please follow other related articles on the PHP Chinese website!