How do I disable assets caching in Yii2?

2019-02-21 13:43发布

I'm writing my first Yii2 application and I want to disable the assets caching, while I'm developing.

Can I disable the caching using the ./config/ .php files?

标签: yii2
1条回答
看我几分像从前
2楼-- · 2019-02-21 14:34

1) Globally it's possible with help of AssetMananer. There is special option $forceCopy for this.

You can set it like this with component:

use Yii;

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

Or in application config:

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

2) If you want disable caching in specific AssetBundle, use $publishOptions property:

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

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

Alternatively you can specify this like in option 1 with help of bundles property. For example:

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

But this:

'forceCopy' => YII_DEBUG,

is more flexible, because it disables this asset bundle caching only in debug mode, but allows on production server. YII_DEBUG is set in web/index.php.

查看更多
登录 后发表回答