Joe
Joe

Reputation: 2591

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

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?

Upvotes: 12

Views: 13847

Answers (5)

annapp
annapp

Reputation: 81

In YII 1 in config we have:

'components'=> [
...
 'assetManager' => array(
            'forceCopy' => YII_DEBUG,
...
)
...

]

Upvotes: 2

jlapoutre
jlapoutre

Reputation: 1787

If you're using Yii2 there is a much simpler solution through configuration.

Add the following to your 'config/web.php':

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    // ...
    $config['components']['assetManager']['forceCopy'] = true;
}

This forces the AssetManager to copy all folders on each run.

Upvotes: 11

marcovtwout
marcovtwout

Reputation: 5269

Re-publishing assets on every request potentially takes a lot of resources and is unnessecary for development.

Only fall back to one of the other solutions if for some reason you cannot use symbolic links on your development machine (not very likely).

Upvotes: 2

Michael Härtl
Michael Härtl

Reputation: 8587

An alternatively solution is to publish your module assets like this:

Yii::app()->assetManager->publish($path, false, -1, YII_DEBUG);

The fourth parameter enforces a copy of your assets, even if they where already published. See the manual on publish() for details.

Upvotes: 3

Willem Renzema
Willem Renzema

Reputation: 5187

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.

Upvotes: 22

Related Questions