DEV Community

Cover image for Creating an application in Yii3 - part 2 the concept of configuration.
Wilmer Arambula
Wilmer Arambula

Posted on

Creating an application in Yii3 - part 2 the concept of configuration.

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 their composer.json.
  • Writes a merge plan into config/.merge-plan.php. It includes configuration from each package composer.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.
Enter fullscreen mode Exit fullscreen mode

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": [
        "console": [
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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



return [
    'app' => [
        'charset' => 'UTF-8',
        'locale' => 'en',
Enter fullscreen mode Exit fullscreen mode
file: application-parameters.php



use App\ApplicationParameters;

/** @var array $params */

return [
    ApplicationParameters::class => [
        'class' => ApplicationParameters::class,
        'charset()' => [$params['app']['charset']],
        'name()' => [$params['app']['name']],
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

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],
Enter fullscreen mode Exit fullscreen mode

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.

Team Developer YiiFramework
Twitter follow

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<