14

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?

arogachev
  • 33,150
  • 7
  • 114
  • 117
Kupigon
  • 307
  • 1
  • 3
  • 9

1 Answers1

26

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.

arogachev
  • 33,150
  • 7
  • 114
  • 117
  • Strange. I've added `'authManager' => [ 'class' => 'yii\web\AssetManager', 'forceCopy' => true, ],` in `'components'` (console.php and web.php), but it still caches in `web/assets`. – Kupigon Jan 24 '15 at 04:03
  • Disabling in this context means that folder also will be presented in assets, but on every page load its content will be forcibly copied, so it will always contain the actual versions of the files. If you don't want that, take a look at this methods. http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJs%28%29-detail, http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile%28%29-detail. Official documentation recommends using assets instead. – arogachev Jan 24 '15 at 07:29
  • 1
    May be `assetManager` instead `authManager`? – verybadbug Feb 24 '15 at 06:44