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

14

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

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


你在使用Yii还是Yii2? - arogachev
1个回答

26

1) 全球范围内可以通过AssetManager进行帮助。有特殊选项$forceCopy来实现这一点。

您可以通过组件设置它,如下所示:

use Yii;

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

或者在应用程序配置中:

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

2)如果您想在特定的AssetBundle中禁用缓存,请使用$publishOptions属性:

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

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

或者您可以使用 捆绑 属性来指定此项,就像选项1一样。例如:

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

但是这个:

'forceCopy' => YII_DEBUG,

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


奇怪。我已经在 'components'(console.php 和 web.php)中添加了 'authManager' => [ 'class' => 'yii\web\AssetManager', 'forceCopy' => true, ],,但它仍然缓存在 web/assets 中。 - Kupigon
在这个上下文中,“禁用”意味着该文件夹也将出现在资源中,但在每次页面加载时,它的内容将被强制复制,因此它将始终包含文件的实际版本。 如果您不想这样做,请查看以下方法:http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJs()-detail,http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail。 官方文档建议改用资源。 - arogachev
1
可以使用 assetManager 替代 authManager 吗? - verybadbug

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接