About independent configuration and dynamic configuration methods in thinkPHP5.0 framework

不言
Release: 2023-03-31 09:08:01
Original
1557 people have browsed it

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,
],
Copy after login

If necessary For an independent configuration file, first add the configuration in config.php:

'extra_config_list'   => ['database'],
Copy after login

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,
],
Copy after login

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');
Copy after login

To obtain the parameters of the complete independent configuration file, use:

Config::get('database');
Copy after login

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('配置参数','配置值');
Copy after login

can also be set in batches, for example:

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

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!