What are the thinkphp configuration types? How to do it?

Source: Internet
Author: User
One, what are the thinkphp configuration types?

1. In thinkphp, there are 6 configurations. That is, custom configuration, application configuration, extended configuration, module configuration, scene configuration, dynamic configuration.

2. The custom configuration is the system default configuration.

3. Application configuration is the individual configuration of the app that we developed ourselves.

4. The extended configuration is primarily to implement the configuration that is used by some extensions in our project during the development process.

5. Module configuration is the configuration of each module in one application.

6. The scenario configuration is primarily to implement the configurations that we set up in different development environments. If we work in the home, the computer environment may be different, the company inside the office, the company's computer environment is different.

7. Dynamic configuration in our module to develop the implementation of the dynamic configuration. As we define the configuration of the database in one controller.

Ii. What is the implementation principle of thinkphp configuration?

1.thinkphp implementation of such different types of configuration, to a large extent, the above aspects of our modular configuration, enhance the flexibility of configuration.

The 2.thinkphp configuration load order is mainly in the following order. Dynamic Configuration "Module configuration" Extended Configuration "application Configuration" Custom configuration

The 3.thinkphp implementation configuration primarily uses the PHP Array_merage () function (if two or more array elements have the same key name, the last element overwrites the other elements.) ). When our system goes to load our configuration file, it overwrites the previous configuration file, so the order of execution for the second step above is implemented.

Third, why do you want to implement such a configuration?

1. Through different types of configurations, we facilitate the configuration management of the project. Join us using the framework's own configuration, when we are in a module development, we need to put the data on another server database, is not our project can not be implemented. There are a lot of things like this, and that's just a simple example.

Iv. Configuration file Requirements

In all the configuration files are in the way of arrays, when we load the configuration file, the system will return an array, this array is our configuration item. We access configuration items in the system, you can use the Config () helper function, or you can use the Config class

Five, how to implement these types of configuration?

1. Formula configuration.

The routines are configured directly in the convention.php below the core directory of the framework, and are not spoken here.

2. Apply the configuration.

When we create a project, the framework defaults to creating configuration files such as config.php,database.php in the west of the application directory. We can do it directly in the appropriate file. The following separate explanation is mainly used to create a directory to configure, the effect is the same, nothing more than a file path.

A. Define the profile path, locate the project portal file, and create a config directory under the application same level directory (the following configuration is done under this directory, which is not explained separately below).

Define (' Conf_path ', __dir__. /.. /config/');

Next, create the configuration file directly below the directory, and the file name created is not necessarily limited to config,database. The configuration items in the file are consistent with the convention.php configuration file. When we are in the process of running the project, the configuration items under that directory are automatically loaded.

3. Extend the configuration. Extended configuration is mainly used in the development of some of its data extension use, such as we use the mailbox, to relate to the mailbox account. Can be configured under this directory. We create a extra directory under the Config directory to create the corresponding file under the directory. As I created the email.php file below, define the following:

<?phpreturn ["user_emial" = "1005349393@qq.com"];

Load the configuration item in our system.

Var_dump (config ("email.user_emial")); In the system default, we will add a PHP prefix to our environment variables, and will convert the configuration items to uppercase. Here we use our helper functions to directly access the configuration items.

4. Module configuration. Here is a general explanation. The main thing is to create the corresponding config.php,database.php and other files under our module directory.

  5. Scene configuration. This is the big trick of this article. scene configuration is mainly to adapt to our different development environment, such as our environment in the company and the environment at home inconsistent configuration, at this time we may need to modify a lot of places, but also back to the company has to be modified, so it is not very troublesome.

First, we load a configuration item in the configuration file below the Config directory.

' app_status '             = ' office ',///The value of the configuration item here is our corresponding configuration environment.

Next, create a office.php under our config directory ( the file name must be exactly the same as the value of the configuration item above )

Finally, we can configure it directly in our office.php configuration file. In fact, there is a relatively good function in thinkphp is. Env. Before always know Laravel have, no major thinkphp5, recently discovered. Using this feature is a lot more convenient for the configuration in development. Great location.

First we create an. env file under our project directory, which configures our configuration items. As follows

status= Office

If we want to use it, we can use the Env class that comes with the frame directly. For more information about Env, refer directly to the development manual.

' App_status ' = Env::get ("status"); we set the value in the previous scene configuration item to the configuration item in the. Env file. Be sure to introduce the Env class.

With our. env file, we only need to configure some of the configuration files, or put some common configuration files in the. env file, we will just modify this one file.

6. Dynamic configuration. Dynamic configuration, as the name implies, is configured as needed for our project development, rather than directly using the configuration in our configuration file. A simple example is listed below.

<?phpnamespace app\index\controller;use Think\controller;class Index extends controller{public function index ()    {        config ("app_name", "Weixin");//Configure a App_name configuration item in the controller, which can also be manipulated using the Config class.    }}

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.