How do I disable assets caching in Yii2?

后端 未结 1 1923
南方客
南方客 2021-01-17 10:25

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?

1条回答
  •  梦毁少年i
    2021-01-17 11:03

    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.

    0 讨论(0)
提交回复
热议问题