Configuration

You can configure application configuration parameters in the bean.php file

return [
    'config'   => [
        'path' => __DIR__ . '/../config',
    ],
];
  • Path Custom configuration file paths
  • Base master file name, defaultbase
  • Type profile types, which php also support formatting by yaml default
  • Parser configuration parser, the PHP/YAML parser has been configured by default.
  • Env Configure the current environment, such as pro/pre/....

Data formats

Configuration catalog All configuration files are resolved into an array, but do not recursively merge data, only merge the current directory file data and merge arrays with its file name array key. 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 is parsed and the data is not recursively parsed. When you currently use the Env configuration, the configuration information in the environment directory overrides the same data as the outermost file name. Reminder: The configuration file can use env() functions to read the environment configuration.

Base.php

return [
    'key' => 'value'
];

Data.php

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

/Pro/base.php

return [
    'key' => 'valuePro'
];

/Pro/data.php

return [
    'key' => 'valuePro'
];

As on the configuration file, when the env parameter is not configured, the merged data format is as follows:

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

When the Env=pro parameter is configured, the merged data format is as follows:

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

Use

Frames refer to global functions, annotations, config objects in a variety of ways, using application configuration data.

Function

Global function usageconfig()

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

Annotations

Inject configuration to property values by using annotations in containers.

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 feature, is read the same data in two different ways.

With annotations, it is important to ensure that the class is a bean object (injected into the container through other annotations)

Object

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

use Swoft\Bean\BeanFactory;

$config = BeanFactory::getBean('config');

Common methods for config objects

  • Get (String $key, $default = null) gets parameters
  • Offsetget ($key) Get parameters
  • ....
/docs/2.x/en/config/config.html
progress-bar