In this second post, we will cover the configuration, certainly one of the main parts when creating an app, according to Wikipedia the configuration is defined “as a data set that determines the value of some variables of a computer program or an operating system. These options are usually loaded during the start of the program and in some cases it is necessary to restart it in order to see the changes”.
YiiFramework it has a powerful tool to make configurations easier, when configuring the container di, we present Yii Config, your solution for building configurations.
Yii config is a composer plugin provides assembling of configurations distributed with composer packages. It allows putting configuration needed to use a package right inside thus implementing a plugin system. The package becomes a plugin holding both the code and its configuration.
How it works:
The package consist of two parts: composer plugin and config loader.
After composer updates its autoload file, and that happens after dump-autoload, require, update or remove
, composer plugin:
- Scans installed packages for
config-plugin
extra option in theircomposer.json
. - Writes a merge plan into
config/.merge-plan.php
. It includes configuration from each packagecomposer.json
.
In the application entry point, usually index.php
, we create an instance of config loader and require a configuration we need:
use Yiisoft\Config\Config;
use Yiisoft\Config\ConfigPaths;
$config = new Config(
new ConfigPaths(dirname(__DIR__)), // defined root path.
);
$web = $config->get('web'); // access config web group.
Now once we understand the concept, let’s look at an example of our app template configuration.
The first thing is to define our configuration in the extra section of composer.json
, here we will tell Yii Config, which configuration should collect, let’s see the following example:
"extra": {
"config-plugin-options": {
"source-directory": "config"
},
"config-plugin": {
"common": "common/**.php",
"params": "params.php",
"web": [
"$common",
"web/**.php"
],
"console": [
"$common",
"console/*.php"
],
}
}
source directory: We simply define the path where our configuration files will be, it can be any name in our example we will call it config
, and the plugin will automatically look for the config folder
in the root directory.
config-plugin: In this section we will define our configuration skeleton, let’s see the following example:
\ root directory.
common\ configuration to be used in all defined groups.
- application-parameters.php
- i18n.php
- logger.php
- psr17.php
- router.php
- translator.php
web\ configuration used in the web group.
- application.php
console\ configuration** used in the console group.
params.php
As we can see in the example above, we have defined the configuration of our packages in files .php
(it doesn't matter if they are YiiFramework packages or not), this allows us to define the configuration of each component in a very simple way to understand.
file: params.php
<?php
declare(strict_types=1);
return [
'app' => [
'charset' => 'UTF-8',
'locale' => 'en',
],
];
file: application-parameters.php
<?php
declare(strict_types=1);
use App\ApplicationParameters;
/** @var array $params */
return [
ApplicationParameters::class => [
'class' => ApplicationParameters::class,
'charset()' => [$params['app']['charset']],
'name()' => [$params['app']['name']],
],
];
Now we are going to understand how the definition works inside the .php
file, the first thing we do is define the class to configure in our container di, in this case ApplicationParameters::class
, the other thing we observe is a comment /** @ var array $params */
, here we can see the magic of Yii Config, it automatically allows you to access the parameters defined in the params group
, and thus be able to use them in the configuration.
Using a language analogy, we can translate the previous configuration to php
in:
$applicacionParameters = (new ApplicacionParameters())
→charset($params['app']['charset'])
→name($params['app']['name']);
The syntax would basically be:
class/interface => [
'class' => implementation::class,
// very important all the values, parameters must go between square brackets.
'property' => [value]
'method() => [parameters],
];
Now that we understand the concept in our next post, we will fully describe our configurations, and explain in detail the use of the container di and the factory.
Wilmer Arámbula.
Top comments (0)