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')
);
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'),
);