有没有办法强制 Yii 在每个请求上重新加载模块资产?

2022-01-04 00:00:00 refresh reload php yii assets

我的网站分为不同的模块.每个模块在 /protected/modules/my_module/assets/cssjs 中都有自己特定的 css 或 js 文件,用于 js 文件.Yiis 资产管理器在我第一次使用使用我的资产的页面时创建文件夹.不幸的是,如果我更改了文件中的某个内容 - Yii 不会重新加载我的 css 或 js 文件.我必须手动删除 /projects/assets 文件夹.开发应用的时候真的很烦.

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.

有没有办法强制 Yii 每次请求都重新加载资源?

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

推荐答案

components/Controller.php 中添加以下内容(或调整现有的 beforeAction):

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.

参见: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.

注意:此代码在 beforeAction 中的位置只是放置位置的一个示例.您只需在对 publish() 的任何调用之前将 forceCopy 属性设置为 true,并将其放置在 beforeAction 中即可实现该目标.

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.

相关文章