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

廉价感情. 提交于 2019-12-04 00:33:39

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.

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.

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.

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).

In YII 1 in config we have:

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

]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!