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:212204browse

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:

<config>
<default_module>Index</default_module>
<url_model>2</url_model>
<session_auto_start>1</session_auto_start>
</config>

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 = [
  &#39;user&#39; => [&#39;type&#39;=>1,&#39;name&#39;=>&#39;thinkphp&#39;],
  &#39;db&#39;  => [&#39;type&#39;=>&#39;mysql&#39;,&#39;user&#39;=>&#39;root&#39;,&#39;password&#39;=>&#39;&#39;],
];
// 设置配置参数
Config::set($config);
// 读取二级配置参数
echo Config::get(&#39;user.type&#39;);
// 或者使用助手函数
echo config(&#39;user.type&#39;);

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=&#39;&#39;

Standard xml format file definition:

<config>
<user>
<type>1</type>
<name>thinkphp</name>
</user>
<db>
<type>mysql</type>
<user>root</user>
<password></password>
</db>
</config>
## The #set method also supports secondary configuration, for example:

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

Reading configuration parameters

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

echo Config::get(&#39;配置参数1&#39;);

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

echo config(&#39;配置参数1&#39;);

Read all configuration parameters:

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

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

Config::has(&#39;配置参数2&#39;);

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

echo Config::get(&#39;配置参数.二级参数&#39;);

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