Merging another web.php file in config/web.php of yii2

We are two groups developing a project made in Yii2 basic. The problem we are facing right now is we have different web.php under config. Our group would like to include our web.php (which was renamed to extras.php) inside the web.php of another group. The difference is we added variables under components of $config of web.php. Yes, we can manually add our new variables under components of $config from the other team but we prefer to use separate files that is why we renamed the other web.php to extras.php.

A small preview of web.php looks like this

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language'=>  isset($_SESSION['language_local']) ? $_SESSION['language_local']:'en',
    'components' => [
        'nagios' => [
                    'class' =>  'app\components\nagios',
        ],
        'hostlistsql' => [
                   'class' =>  'app\components\hostlistsql',
        ],
        'request' => [empty) - this is required by cookie validation
            'cookieValidationKey' => 'nYwdMpu-dw004qwFRZmqZOzzC3xdnL8b',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
    'params' => $params,
];

extras.php looks like this

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language'=>  isset($_SESSION['language_local']) ? $_SESSION['language_local']:'en',
    'components' => [
        'user_functions' => [
                    'class' =>  'app\components\UserFunctions',
        ],
        'user_extras' => [
                   'class' =>  'app\components\UserExtras',
        ],
        'request' => [empty) - this is required by cookie validation
            'cookieValidationKey' => 'nYwdMpu-dw004qwFRZmqZOzzC3xdnL8b',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
    ],
    'params' => $params,
];

What approach should we take to include extras.php inside web.php?

EDIT:

SOLUTION: Inside web/index.php has

$config = require(__DIR__ . '/../config/web.php');

I changed it instead to

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../config/web.php'),
    require(__DIR__ . '/../config/extras.php')
);

Answers


Why would you want to include extras.php inside web.php instead of just merging them?

Here is how the same thing is handled in yii2 advanced https://github.com/yiisoft/yii2-app-advanced/blob/master/environments/dev/frontend/web/index.php

As you can see common..main.php is merged with common..main-local.php is merged with frontend..main.php is merged with frontend..main-local.php

There are 4 files that are merged to get to the end 1 single config file. Eazy as pie.

If you really want to merge things inside web.php do a

$config = [....];
return yii\helpers\ArrayHelper::merge(
    $config,
    require(__DIR__ . 'extras.php'),
);

Do you guys have any version control system (like git, Bitbucket, etc...) ? You could let only the things you have in common in the web.php file and the rest coming from a external file.

Since each group will have a different file, i would recommend create a constant that decides which file is being used. Following your example:

web.php:

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'language'=>  isset($_SESSION['language_local']) ? $_SESSION['language_local']:'en',
    'components' => EXTRAS ? require(__DIR__ . '/components2.php') : require(__DIR__ . '/components1.php'),
    'params' => require(__DIR__ . '/params.php')
];

Example of components2.php:

return [
    'user_functions' => [
                'class' =>  'app\components\UserFunctions',
    ],
    'user_extras' => [
               'class' =>  'app\components\UserExtras',
    ],
    'request' => [empty) - this is required by cookie validation
        'cookieValidationKey' => 'nYwdMpu-dw004qwFRZmqZOzzC3xdnL8b',
    ],
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ]
]

And EXTRAS is a constant defined elsewhere. You could do that in web/index.php (follow the YII_DEBUG and YII_ENV samples) and add it in your ignored files, if you didn't already.


Need Your Help

HTML CSS linkage error

html css wordpress horizontal-scrolling horizontalscrollview

I linking the style css in my html code and on all HTML editors my code is working absolutely fine except on my site.

Strongly typed events in Haskell

haskell event-sourcing strong-typing

I'm working on my first 'real' Haskell project, and simultaneously trying to get my head around event sourcing. (It seemed like a good match; event sourcing is a rather functional way of looking at...