在 yii2 的 config/web.php 中合并另一个 web.php 文件
Merging another web.php file in config/web.php of yii2
我们两个小组开发了一个用 Yii2 basic 制作的项目。我们现在面临的问题是 config
下有不同的 web.php
。我们的小组希望将我们的 web.php
(已重命名为 extras.php
)包含在另一个小组的 web.php
中。不同的是我们在web.php
的$config
的components
下添加了变量。是的,我们可以在其他团队的 $config
的 components
下手动添加我们的新变量,但我们更喜欢使用单独的文件,这就是我们将另一个 web.php
重命名为 extras.php
的原因.
web.php 的小预览如下所示
$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 看起来像这样
$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,
];
我们应该采取什么方法将 extras.php
包含在 web.php
中?
编辑:
解决方案:
web/index.php
里面有
$config = require(__DIR__ . '/../config/web.php');
我改为
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../config/web.php'),
require(__DIR__ . '/../config/extras.php')
);
你们有任何版本控制系统吗(如 git、Bitbucket 等...)?
您可以只让 web.php
文件中共有的内容,其余内容来自外部文件。
由于每个组都有不同的文件,我建议创建一个 constant 来决定使用哪个文件。按照你的例子:
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')
];
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',
]
]
而EXTRAS
是在别处定义的常量。您可以在 web/index.php
中执行此操作(遵循 YII_DEBUG
和 YII_ENV
示例)并将其添加到您忽略的文件中,如果您还没有的话。
为什么要将 extras.php 包含在 web.php 中而不只是合并它们?
这是在 yii2 advanced 中如何处理同样的事情 https://github.com/yiisoft/yii2-app-advanced/blob/master/environments/dev/frontend/web/index.php
如您所见,common..main.php 与 common..main-local.php 与 frontend..合并main.php 与 frontend..main-[ 合并=25=]
有 4 个文件被合并到最后 1 个配置文件。像馅饼一样容易。
如果你真的想合并里面的东西 web.php 做一个
$config = [....];
return yii\helpers\ArrayHelper::merge(
$config,
require(__DIR__ . 'extras.php'),
);
我们两个小组开发了一个用 Yii2 basic 制作的项目。我们现在面临的问题是 config
下有不同的 web.php
。我们的小组希望将我们的 web.php
(已重命名为 extras.php
)包含在另一个小组的 web.php
中。不同的是我们在web.php
的$config
的components
下添加了变量。是的,我们可以在其他团队的 $config
的 components
下手动添加我们的新变量,但我们更喜欢使用单独的文件,这就是我们将另一个 web.php
重命名为 extras.php
的原因.
web.php 的小预览如下所示
$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 看起来像这样
$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,
];
我们应该采取什么方法将 extras.php
包含在 web.php
中?
编辑:
解决方案:
web/index.php
里面有
$config = require(__DIR__ . '/../config/web.php');
我改为
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../config/web.php'),
require(__DIR__ . '/../config/extras.php')
);
你们有任何版本控制系统吗(如 git、Bitbucket 等...)?
您可以只让 web.php
文件中共有的内容,其余内容来自外部文件。
由于每个组都有不同的文件,我建议创建一个 constant 来决定使用哪个文件。按照你的例子:
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')
];
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',
]
]
而EXTRAS
是在别处定义的常量。您可以在 web/index.php
中执行此操作(遵循 YII_DEBUG
和 YII_ENV
示例)并将其添加到您忽略的文件中,如果您还没有的话。
为什么要将 extras.php 包含在 web.php 中而不只是合并它们?
这是在 yii2 advanced 中如何处理同样的事情 https://github.com/yiisoft/yii2-app-advanced/blob/master/environments/dev/frontend/web/index.php
如您所见,common..main.php 与 common..main-local.php 与 frontend..合并main.php 与 frontend..main-[ 合并=25=]
有 4 个文件被合并到最后 1 个配置文件。像馅饼一样容易。
如果你真的想合并里面的东西 web.php 做一个
$config = [....];
return yii\helpers\ArrayHelper::merge(
$config,
require(__DIR__ . 'extras.php'),
);