Application configuration

Bean configuration

The application configuration data is managed by a bean object, and the application configuration parameters can be set in the app/bean.php file.

 return [
    'config'   => [
        'path' => __DIR__ . '/../config',
    ],
]; 

Configurable items:

  • path custom configuration file path
  • base main file name, default base ( other file data will be merged into the main file data by file name key )
  • type configuration file type, the default php also supports yaml format
  • parser configuration parser, php/yaml parser is configured by default.
  • env configures the current environment such as dev/test/pre/pro

Data Format

All configuration files in the configuration directory will be parsed into an array, but the data will not be merged recursively. Only the current directory file data will be merged, and the file name will be merged into an array with the file name. For example, the config directory configuration file is as follows:

 |-- base.php
|-- data.php
`-- pro
    |-- base.php
    `-- data.php 

Only the current directory file data will be parsed and the data will not be parsed recursively. When the env configuration is currently used, the configuration information in the environment directory will overwrite the data with the same name as the outermost file. Reminder: The configuration file can be used to read the environment configuration using the env() function.

  • config/base.php
 return [
    'key' => 'value'
]; 
  • config/data.php
 return [
    'dkey' => [
        'dvalue'
    ],
    'key' => 'value'
]; 
  • config/pro/base.php
 return [
    'key' => 'valuePro'
]; 
  • config/pro/data.php
 return [
    'key' => 'valuePro'
]; 

For the above configuration file, when the env parameter of config is not configured, the merged data format is as follows:

 return [
    'key' => 'value',
    'data' => [
        'dkey' => [
            'dvalue'
        ],
        'key' => 'value'
    ]
]; 

When configuring the 'env' => 'pro' parameter of the config object, the merged data format is as follows:

 return [
    'key' => 'valuePro',
    'data' => [
        'dkey' => [
            'dvalue'
        ],
        'key' => 'valuePro'
    ]
]; 

Get configuration

The framework provides global functions, annotations, and config objects in multiple ways, using application configuration data.

function

Global function uses config()

 config(string $key = null, mixed $default = null):mixed 
  • Key configuration parameter key, sub-array can be used . Split, such as the above example data.dkey can get ["dvalue"] , when key=null get all configuration parameters
  • Default default parameter, if the key parameter does not exist, return the default value, the default value can be any type

annotation

Inject the configuration to the attribute value by using the annotations in the container.

 use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Config\Annotation\Mapping\Config;

/**
* @Bean()
*/
class Demo
{
    /**
    * @Config("data.dkey")
    */
    private $dvalue = [];

    // ...
} 

This example, like the above, reads the same data in two different ways.

With annotations, be sure to make the class a bean object (injected into the container via other annotations)

Object

If the above two methods still can't meet your business needs, you can get the configuration object from the container, which comes with many ways to manipulate the configuration data.

 $config = \Swoft::getBean('config'); 

Common method of config object

  • Get(string $key, $default = null) Get parameters
  • offsetGet($key) Get the parameter
  • ....
/docs/2.x/en/config/config.html
progress-bar