Home Backend Development PHP Tutorial thinkPHP5.0 framework configuration format, loading, parsing and reading methods

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

Jun 07, 2018 pm 05:43 PM
read

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:

1

2

3

4

5

6

7

8

9

10

11

//项目配置文件

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

1

2

3

4

5

6

7

8

9

//项目配置文件

return [

  'cache'         => [

    'type'  => 'File',

    'path'  => CACHE_PATH,

    'prefix' => '',

    'expire' => 0,

  ],

];

Copy after login

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:

1

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:

1

2

3

4

5

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

1

2

3

4

5

{

"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:

1

2

3

4

5

6

7

8

9

10

$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):

1

2

3

4

5

6

7

[user]

type=1

name=thinkphp

 [db]

type=mysql

user=rot

password=&#39;&#39;

Copy after login

Standard xml format file definition:

1

2

3

4

5

6

7

8

9

10

11

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

1

2

3

4

Config::set([

  &#39;type&#39;   => &#39;file&#39;,

  &#39;prefix&#39;  => &#39;think&#39;

],&#39;cache&#39;);

Copy after login

Reading configuration parameters

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

1

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:

1

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

Copy after login

Read all configuration parameters:

1

2

dump(Config::get());

// 或者 dump(config());

Copy after login

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

1

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

Copy after login

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

1

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
How to read txt file correctly using pandas How to read txt file correctly using pandas Jan 19, 2024 am 08:39 AM

How to use pandas to read txt files correctly requires specific code examples. Pandas is a widely used Python data analysis library. It can be used to process a variety of data types, including CSV files, Excel files, SQL databases, etc. At the same time, it can also be used to read text files, such as txt files. However, when reading txt files, we sometimes encounter some problems, such as encoding problems, delimiter problems, etc. This article will introduce how to read txt correctly using pandas

Practical tips for reading txt files using pandas Practical tips for reading txt files using pandas Jan 19, 2024 am 09:49 AM

Practical tips for reading txt files using pandas, specific code examples are required. In data analysis and data processing, txt files are a common data format. Using pandas to read txt files allows for fast and convenient data processing. This article will introduce several practical techniques to help you better use pandas to read txt files, along with specific code examples. Reading txt files with delimiters When using pandas to read txt files with delimiters, you can use read_c

Practical methods for reading web page data with Pandas Practical methods for reading web page data with Pandas Jan 04, 2024 am 11:35 AM

The practical method of reading web page data in Pandas requires specific code examples. During data analysis and processing, we often need to obtain data from web pages. As a powerful data processing tool, Pandas provides convenient methods to read and process web page data. This article will introduce several commonly used practical methods for reading web page data in Pandas, and attach specific code examples. Method 1: Use the read_html() function. Pandas’ read_html() function can read directly from the web page.

Example of reading and writing CSV files using OpenCSV in Java Example of reading and writing CSV files using OpenCSV in Java Dec 20, 2023 pm 01:39 PM

Example of using OpenCSV to read and write CSV files in Java. CSV (Comma-SeparatedValues) refers to comma-separated values ​​and is a common data storage format. In Java, OpenCSV is a commonly used tool library for reading and writing CSV files. This article will introduce how to use OpenCSV to implement examples of reading and writing CSV files. Introducing the OpenCSV library First, you need to introduce the OpenCSV library to

How to read Excel files with PHP and answers to common questions How to read Excel files with PHP and answers to common questions Jun 09, 2023 am 11:41 AM

How to read Excel files with PHP and FAQs Excel is a very common spreadsheet file format, and many businesses and data are stored in Excel files. During the development process, if you need to import the data in the Excel file into the system, you need to use PHP to read the Excel file. This article will introduce how to read Excel files with PHP and answer common questions. 1. How to read Excel files with PHP 1. Use the PHPExcel class library PHPExcel is a P

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

How to read binary files in Golang? How to read binary files in Golang? Mar 21, 2024 am 08:27 AM

How to read binary files in Golang? Binary files are files stored in binary form that contain data that a computer can recognize and process. In Golang, we can use some methods to read binary files and parse them into the data format we want. The following will introduce how to read binary files in Golang and give specific code examples. First, we need to open a binary file using the Open function from the os package, which will return a file object. Then we can make

Getting started with PHP file processing: step-by-step guide to reading and writing Getting started with PHP file processing: step-by-step guide to reading and writing Sep 06, 2023 am 09:58 AM

Getting started with PHP file processing: Step-by-step guide for reading and writing In web development, file processing is a common task, whether it is reading files uploaded by users or writing the results to files for subsequent use. Understand how to use PHP Document processing is very important. This article will provide a simple guide to introduce the basic steps of reading and writing files in PHP, and attach code examples for reference. File reading in PHP, you can use the fopen() function to open a file and return a file resource (file

See all articles