Detailed description of the thinkPHP5.0 framework configuration format, loading parsing and reading method sample code

Source: Internet
Author: User
Tags array definition
This article mainly introduces the thinkPHP5.0 framework configuration format, load parsing and reading method, combined with the example form to analyze the common format of thinkPHP5.0 framework configuration, load parsing method, reading method and other related operation skills, the need for friends can refer to the next

This paper describes the thinkPHP5.0 framework configuration format, load parsing and reading methods. Share to everyone for your reference, as follows:

thinkphp supports configuration formats in multiple formats, but ultimately it is a way to parse into a PHP array.

PHP Array Definition

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

project configuration file return [  //default module name  ' default_module ' = ' = '    index ',  //Default controller name  ' default_controller '  = > ' index ',  //Default action name  ' default_action '    = ' index ',  //More configuration Parameters//  ...];

The configuration parameter names are case-insensitive (because the case definition is converted to lowercase), the new recommendation is to use lowercase to define specifications for configuration parameters.

You can also use a two-dimensional array in a configuration file to configure more information, such as:

project configuration file return [  ' cache '         = = [    ' type '  = ' file ',    ' path '  = = Cache_path,    ' prefix ' + ',    ' expire ' = 0,  ],;

Additional Configuration format support

In addition to using native PHP arrays, other formats, such as Json/xml/ini, can also be supported (by way of a driver extension).

For example, we can read the JSON configuration file using the following method:

Config::p arse (app_path. ' Config/config.json ');

INI Format Configuration example:

Default_module=index; default Module
url_model=2; URL pattern
Session_auto_start=on; open session

XML Format Configuration example:

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

JSON format Configuration example:

{"Default_module": "Index", "Url_model": 2, "Session_auto_start": True}

Second-level configuration

Configuration parameters support Level Two, for example, the following is an example of a level two configuration setting and reading:

$config = [' user ' = = ['  type ' =>1, ' name ' = ' thinkphp '],  ' db ' = =  [' type ' = ' mysql ', ' user ' = > ' root ', ' password ' = '],];//set configuration Parameters Config::set ($config);//read level Two configuration parameters echo config::get (' User.type ');// or use the Assistant Function echo config (' user.type ');

The system does not support the reading of configuration parameters above the second level and requires manual step reading.

Operation with a two-level configuration is still supported in the case of a scope.

If you use a configuration file in a different format, the level two configuration is defined as follows (for example, INI and XML):

[user]type=1name=thinkphp [db]type=mysqluser=rotpassword= ']

The 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 level two 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 (' configuration parameter 1 ');

The system defines an assistant config for the Get method, which can be simplified to:

echo config (' configuration parameter 1 ');

Read all the configuration parameters:

Dump (Config::get ());//or Dump (Config ());

Or you need to determine if there is a setting parameter:

Config::has (' configuration parameter 2 ');

If you need to read a level two configuration, you can use:

echo config::get (' configuration parameters. Two-level parameters ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.