Is there a way to force Yii to reload module assets on every request?

Joe picture Joe · Mar 25, 2013 · Viewed 12.4k times · Source

My website is divided into separate modules. Every module has it's own specific css or js files in /protected/modules/my_module/assets/css or js for js files. Yiis assets manager creates folder when I first use page that uses my assets. Unfortunately if I change sth in my files - Yii does not reload my css or js file. I have to manually delete /projects/assets folder. It is really annoying when you are developing the app.

Is there a way to force Yii to reload assets every request?

Answer

Willem Renzema picture Willem Renzema · Mar 25, 2013

In components/Controller.php add the following (or adjust an existing beforeAction):

protected function beforeAction($action){
    if(defined('YII_DEBUG') && YII_DEBUG){
        Yii::app()->assetManager->forceCopy = true;
    }
    return parent::beforeAction($action);
}

What this does it that before any actions are started, the application will check to see if you are in debug mode, and if so, will set the asset manager to forcibly recopy all the assets on every page load.

See: http://www.yiiframework.com/doc/api/1.1/CAssetManager#forceCopy-detail

I have not tested this, but based on the documentation I believe it should work fine.

Note: The placement of this code within beforeAction is just an example of where to put it. You simply need to set the forceCopy property to true before any calls to publish(), and placing it in beforeAction should accomplish that goal.