如何在 Yii2 中禁用资产缓存?

How do I disable assets caching in Yii2?

我正在编写我的第一个 Yii2 应用程序,我想在开发时禁用资产缓存。

我可以使用 ./config/ .php 文件禁用缓存吗?

1) 在全球范围内,AssetMananer. There is special option $forceCopy 的帮助是可能的。

你可以这样设置组件:

use Yii;

Yii::$app->assetManager->forceCopy = true;

或在应用程序配置中:

'components' => [
    'assetManager' => [
        'class' => 'yii\web\AssetManager',
        'forceCopy' => true,          
    ],
],

2) 如果要在特定 AssetBundle, use $publishOptions 中禁用缓存 属性:

public $sourcePath = '...' // In order to use $publishOptions you should specify correct source path.

public $publishOptions = [
    'forceCopy' => true,
];

或者,您可以在 bundles 属性 的帮助下像选项 1 中那样指定。例如:

'components' => [
    'assetManager' => [
        'class' => 'yii\web\AssetManager',
        'forceCopy' => true,          
        'bundles' => [
            'yii\bootstrap\BootstrapAsset' => [
                'forceCopy' => true,
            ],
        ],
    ],
],

但是这个:

'forceCopy' => YII_DEBUG,

更灵活,因为它仅在调试模式下禁用此资产包缓存,但在生产服务器上允许。 YII_DEBUG 设置在 web/index.php.