Yii2 覆盖嵌套配置参数

Ser*_*nko 0 merge configuration yii2

我以这种方式合并我的配置:

$config = \yii\helpers\ArrayHelper::merge(
    (require (__DIR__ . '/../config/web.php')),
    (require __DIR__ . '/../config/overrides/web.php')
);
Run Code Online (Sandbox Code Playgroud)

这是config/web.php

$config = [
    'components' => [
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
                [
                    'class' => 'yii\log\EmailTarget',
                    'levels' => ['info'],
                    'categories' => ['parsingFailure'],
                    'logVars' => [],
                    'message' => [
                        'from' => ['system@host.com'],
                        'to' => ['support@host.com'],
                        'subject' => 'Message parsing failure',
                    ],
                ],
            ],
        ],
        //....some more components
    ]
];
Run Code Online (Sandbox Code Playgroud)

这是我尝试应用的覆盖config/overrides/web.php

$config = [
    'components' => [
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [],
        ],
    ]
];
Run Code Online (Sandbox Code Playgroud)

所以,我的目标是禁用本地配置中的日志记录。当然它不起作用,因为array_merge的行为不同并且没有任何内容被覆盖。

top*_*her 5

来自文档

如果两个数组都具有数组类型的元素并且具有相同的键,则将进行递归合并。[...] 您可以使用yii\helpers\UnsetArrayValue对象取消以前数组中的值或yii\helpers\ReplaceArrayValue强制替换以前的值而不是递归合并。

所以你的第二个数组应该是

'targets' =>  new \yii\helpers\ReplaceArrayValue([]),
Run Code Online (Sandbox Code Playgroud)