Home  >  Article  >  Backend Development  >  thinkPHP5.0 framework configuration format, loading, parsing and reading methods

thinkPHP5.0 framework configuration format, loading, parsing and reading methods

不言
不言Original
2018-06-07 17:43:212065browse

This article mainly introduces the thinkPHP5.0 framework configuration format, loading parsing and reading methods, and combines examples with a detailed analysis of the common formats of thinkPHP5.0 framework configuration, loading parsing methods, reading methods and other related operating techniques. Friends who need it can refer to

. The examples in this article describe the thinkPHP5.0 framework configuration format, loading parsing and reading methods. Share it with everyone for your reference, the details are as follows:

ThinkPHP supports multiple formats of configuration formats, but they are all ultimately parsed into PHP arrays.

PHP array definition

The way to return a PHP array is the default configuration definition format, for example:

//项目配置文件
return [
  // 默认模块名
  'default_module'    => 'index',
  // 默认控制器名
  'default_controller'  => 'Index',
  // 默认操作名
  'default_action'    => 'index',
  //更多配置参数
  //...
];

Configuration parameter names are not case-sensitive ( Because definitions in upper and lower case will be converted to lower case), the new version recommends using lower case to define configuration parameter specifications.

You can also use a two-dimensional array in the configuration file to configure more information, for example:

//项目配置文件
return [
  'cache'         => [
    'type'  => 'File',
    'path'  => CACHE_PATH,
    'prefix' => '',
    'expire' => 0,
  ],
];

Other configuration formats are supported

In addition In addition to using native PHP arrays, you can also use other format support such as json/xml/ini (extended through drivers).

For example, we can use the following method to read the json configuration file:

Config::parse(APP_PATH.'config/config.json');

ini format Configuration example:

DEFAULT_MODULE=Index ;Default module
URL_MODEL=2 ;URL mode
SESSION_AUTO_START=on ;Whether to open session

xml formatConfiguration example:


Index
2
1

json formatConfiguration example:

{
"default_module":"Index",
"url_model":2,
"session_auto_start":True
}

Secondary configuration

Configuration parameters support level 2. For example, the following is an example of setting and reading level 2 configuration:

$config = [
  'user' => ['type'=>1,'name'=>'thinkphp'],
  'db'  => ['type'=>'mysql','user'=>'root','password'=>''],
];
// 设置配置参数
Config::set($config);
// 读取二级配置参数
echo Config::get('user.type');
// 或者使用助手函数
echo config('user.type');

The system does not support reading configuration parameters above level 2 and needs to be read manually step by step.

With scope, secondary configuration operations are still supported.

If configuration files in other formats are used, the secondary configuration is defined as follows (taking ini and xml as examples):

[user]
type=1
name=thinkphp
 [db]
type=mysql
user=rot
password=''

Standard xml format file definition:



1
thinkphp


mysql
root


## The #set method also supports secondary configuration, for example:

Config::set([
  'type'   => 'file',
  'prefix'  => 'think'
],'cache');

Reading configuration parameters

After setting the configuration parameters, you can use the get method to read the configuration. For example:

echo Config::get('配置参数1');

The system defines an assistant config for the get method. The above can be simplified to:

echo config('配置参数1');

Read all configuration parameters:

dump(Config::get());
// 或者 dump(config());

Or you need to determine whether There is a certain setting parameter:

Config::has('配置参数2');

If you need to read the secondary configuration, you can use:

echo Config::get('配置参数.二级参数');

The above is the entire content of this article. I hope it will be helpful to everyone's learning. More related Please pay attention to the PHP Chinese website for content!

Related recommendations:

How to execute native SQL statements in thinkPHP framework

ThinkPHP5 framework simply implements batch queries

How to automatically generate modules and directories for Thinkphp5.0

The above is the detailed content of thinkPHP5.0 framework configuration format, loading, parsing and reading methods. 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