Sample code sharing for independent configuration and dynamic configuration of thinkPHP5.0 framework

Source: Internet
Author: User
This article mainly introduces the independent configuration and dynamic configuration methods of the thinkPHP5.0 framework, and analyzes the functions, implementation skills and related precautions of the independent configuration and static configuration of the thinkPHP5.0 framework in combination with the instance form, for more information about independent configuration and dynamic configuration of thinkPHP5.0, see the next article, the functions, implementation skills, and precautions of independent configuration and static configuration of the thinkPHP5.0 framework are analyzed based on the instance form. For more information, see

This article describes the independent configuration and dynamic configuration methods of the thinkPHP5.0 framework. We will share this with you for your reference. The details are as follows:

Independent configuration file:

The new version supports configuration file separation. you only need to configureextra_config_listParameters (in the application public configuration file ).

For example, if you do not use an independent configuration file, the database configuration information should be configured in config. php as follows:

/* Database settings */'database' => [// database type 'type' => 'mysql', // server address 'hostname' => '2017. 0.0.1 ', // database name 'database' => 'thinkphp', // database username 'username' => 'root ', // database password 'password' => '', // database connection port 'hostport' =>'', // database connection parameter 'params' => [], // The database encoding uses utf8 'charset' => 'utf8' by default, // The database table prefix 'prefix' => '', // database debugging mode 'debug' => false,],

To use an independent configuration file, first add the configuration in config. php:

'extra_config_list'   => ['database'],

After definition, the database configuration can use the database. php file independently. the configuration content is as follows:

/* Database settings */return [// Database type 'type' => 'mysql', // server address 'hostname' => '2017. 0.0.1 ', // database name 'database' => 'thinkphp', // database username 'username' => 'root ', // database password 'password' => '', // database connection port 'hostport' =>'', // database connection parameter 'params' => [], // The database encoding uses utf8 'charset' => 'utf8' by default, // The database table prefix 'prefix' => '', // database debugging mode 'debug' => false,],

If the extra_config_list parameter is configured in the config. php and database. php files, the configuration in the database. php file overwrites the settings in config. php.

Parameters in the independent configuration file are obtained in two-dimensional configuration mode. for example, to obtain the type parameter of the independent configuration file of the database, it should be:

Config::get('database.type');

To obtain the complete parameters of the independent configuration file, use:

Config::get('database');

Two independent configuration files, including database and validate, are set by default for the database configuration and validation rule definitions respectively.

Dynamic Configuration:

Set configuration parameters

Use the set method to dynamically set parameters, for example:

Config: set ('configuration parameters', 'configuration value'); // or use the helper function config ('configuration parameters', 'configuration value ');

You can also set them in batches, for example:

Config: set (['configuration parameter 1' => 'configuration value', 'configuration parameter 2' => 'configuration value']); // or use the helper function config (['configuration parameter 1' => 'configuration value', 'configuration parameter 2' => 'configuration value']);

The above is the details shared by the instance code of independent configuration and dynamic configuration of thinkPHP5.0 framework. For more information, see other related articles in the first PHP community!

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.