Analysis of configuration loading and reading configuration example of ThinkPHP framework configuration operation

Time:2020-10-25

This article describes the configuration loading and reading configuration of ThinkPHP framework configuration operation. For your reference, the details are as follows:

Configuration loading

In ThinkPHP, generally speaking, application configuration files are loaded automatically. The loading order is as follows:

Convention configuration > application configuration > mode configuration > debugging configuration > state configuration > module configuration > extended configuration > dynamic configuration

The above is the loading order of the configuration file. Since the later configuration will override the previous configuration with the same name (without taking effect), the priority of the configuration is from right to left.

Differences and locations of different configuration files:

Convention configuration

Convention is more important than configuration, which is an important idea for the system to followThinkPHP/Conf/convention.php)The common parameters are configured by default according to most usage. Therefore, for application configuration files, you only need to configure configuration parameters that are different from or new to the Convention. If you completely adopt the default configuration, you may not even need to define any configuration files

Application configuration

The application configuration file is the public configuration file that is loaded first before all modules are called (the default is inApplication/Common/Conf/config.php)。

If you change the name of the common module, the location of the public configuration file changes accordingly

Mode configuration (optional)

If you use an application mode other than the normal application mode, you can also define a configuration file for the application mode (described later). The file naming standard is:Application/Common/Conf/config_ Application mode name.php(it will only be loaded if this mode is running).

The schema profile is optional

Debug configuration (optional)

If debug mode is enabled, the framework’s debug configuration file (located inThinkPHP/Conf/debug.php)And application debugging configuration file (located inApplication/Common/Conf/debug.php

State configuration (optional)

Each application can set its own state (or called application scenario) in different situations and load different configuration files.

For example, you need to set up different database test environments in your company and at home. In the company environment, we define in the entry file:


define('APP_STATUS','office');

The configuration file corresponding to that state (located inApplication/Common/Conf/office.php)。

If we go home, we modify the definition as:


define('APP_STATUS','home');

The configuration file corresponding to that state (located inApplication/Common/Conf/home.php)。

The state profile is optional

Module configuration

Each module automatically loads its own configuration file (located in theApplication / current module name / conf/ config.php)。

If other application modes other than the normal mode are used, you can also define the configuration file for the application mode separately. The naming standard is as follows:Application / current module name / conf / config_ Application mode name.php(it will only be loaded if this mode is running).

The module can also support independent state configuration files. The naming rules are as follows:Application / current module name / conf / application status.php

Read configuration

No matter what kind of configuration file is defined, the C method provided by the system (you can use the word “config” to help you remember) is used to read the existing configuration.

Usage:

C ('parameter name ')

For example, read the current URL mode configuration parameter:

$model = C('URL_MODEL');
//Since configuration parameters are case insensitive, the following notation is equivalent
// $model = C('url_model');

However, it is recommended to use the upper case specification.

Note: configuration parameter names cannot contain “.” and special characters. Letters, numbers and underscores are allowed.

If URL_ Model does not exist, null is returned.

It supports setting default values when reading, for example:

//If my_ If config has not been set, default is returned_ Config string
C('my_config',null,'default_config');

The C method can also be used to read two-dimensional configurations:

//Gets the user type settings in the user configuration
C('USER_CONFIG.USER_TYPE');

Because the configuration parameters are globally valid, the C method can read any configuration anywhere, even if a setting parameter has expired.

More about ThinkPHP related content, interested readers can view this site topic: “ThinkPHP introduction tutorial”, “ThinkPHP template operation skills summary”, “ThinkPHP common methods summary”, “CodeIgniter introductory tutorial”, “Ci (CodeIgniter) framework advanced tutorial”, “Zend framework framework introduction tutorial” and “PHP template technology summary”.

I hope this article will be helpful to the PHP program design based on ThinkPHP framework.

Recommended Today

Comparison and analysis of Py = > redis and python operation redis syntax

preface R: For redis cli P: Redis for Python get ready pip install redis pool = redis.ConnectionPool(host=’39.107.86.223′, port=6379, db=1) redis = redis.Redis(connection_pool=pool) Redis. All commands I have omitted all the following commands. If there are conflicts with Python built-in functions, I will add redis Global command Dbsize (number of returned keys) R: dbsize P: print(redis.dbsize()) […]