Detailed introduction to thinkPHP5.0 framework configuration format, loading sample code of parsing and reading methods

黄舟
Release: 2023-03-06 17:58:02
Original
1265 people have browsed it

This article mainly introduces the thinkPHP5.0frameworkConfiguration format, loading parsing and reading methods, combined with examples, a detailed analysis of the common formats of thinkPHP5.0 framework configuration, loading parsing methods, reading Methods and other related operating skills, friends in need can refer to

This article describes the thinkPHP5.0 framework configuration format, loading parsing and reading methods with examples. 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',
  //更多配置参数
  //...
];
Copy after login

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 two-dimensional array in the configuration file to configure more information, for example:

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

Other configuration formats are supported

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

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

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

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

json formatConfiguration example:

{
"default_module":"Index",
"url_model":2,
"session_auto_start":True
}
Copy after login

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

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

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

Read configurationParameters

After setting the configuration parameters, you can use get The method reads the configuration, for example:

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

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

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

Read all configuration parameters:

dump(Config::get());
// 或者 dump(config());
Copy after login

Or you need to determine whether a certain setting parameter exists:

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

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

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

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