Home  >  Article  >  Backend Development  >  Example code sharing of independent configuration and dynamic configuration of thinkPHP5.0 framework

Example code sharing of independent configuration and dynamic configuration of thinkPHP5.0 framework

黄舟
黄舟Original
2017-03-18 09:39:561436browse

This article mainly introduces the thinkPHP5.0 framework independent configuration and dynamic configuration methods, and analyzes the functions, implementation skills and related of thinkPHP5.0 framework independent configuration and static configuration in the form of examples. Note , friends in need can refer to

This article describes the independent configuration and dynamic configuration methods of thinkPHP5.0 framework through examples. Share it with everyone for your reference, the details are as follows:

IndependentConfiguration file:

The new version supports configuration file separation, only need to configure extra_config_listparameters (in the application public configuration file).

For example, if you do not use an independent 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 configure it in config.php Add configuration:

'extra_config_list'   => ['database'],

After definition, the database configuration can be used independently in the database.php file. 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 both in config.php and database. If both are configured in the php file, 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 , then use:

Config::get('database');

The system has set up two independent configuration files by default, including database and validate, which are used to set the 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('配置参数','配置值');

You can also set them in batches , for example:

Config::set([
  '配置参数1'=>'配置值',
  '配置参数2'=>'配置值'
]);
// 或者使用助手函数
config([
  '配置参数1'=>'配置值',
  '配置参数2'=>'配置值'
]);

The above is the detailed content of Example code sharing of independent configuration and dynamic configuration of thinkPHP5.0 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn