当前位置:Gxlcms > PHP教程 > Yii2为什么要在web/asset生成文件?是否会自动清除?

Yii2为什么要在web/asset生成文件?是否会自动清除?

时间:2021-07-01 10:21:17 帮助过:7人阅读

[root@aliyun assets]# pwd
/home/flxx/basic/web/assets
[root@aliyun assets]# ll
total 60
drwxrwxrwx 7 root   root   4096 Jul 29 13:26 1fe421c3
drwxrwxr-x 7 apache apache 4096 Jul 29 13:39 349e6026
drwxrwxrwx 5 root   root   4096 Jul 29 13:26 40473b7f
drwxrwxr-x 5 apache apache 4096 Jul 29 13:31 4939b2cb
drwxrwxr-x 3 apache apache 4096 Jul 29 13:39 55993b1e
drwxrwxrwx 7 root   root   4096 Jul 29 13:26 68e31155
drwxrwxrwx 5 root   root   4096 Jul 29 13:26 7007e07
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 770d3be4
drwxrwxrwx 3 root   root   4096 Jul 29 13:26 7ee37afb
drwxrwxrwx 3 root   root   4096 Jul 29 13:26 9e44a6d
drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a51215f4
drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a53efc6f
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 bfcc54b5
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 c8cb6423
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 e006560
[root@aliyun assets]#

为什么yii2 要在 web/asset 目录下生成这样的缓存文件?(很多MVC都有这个趋势) 且生成的CSS JS缓存文件,并没有做minify,里面注释还是原样保留。 这些缓存目录文件,何时清除?是否是自动清除?

回复内容:

[root@aliyun assets]# pwd
/home/flxx/basic/web/assets
[root@aliyun assets]# ll
total 60
drwxrwxrwx 7 root   root   4096 Jul 29 13:26 1fe421c3
drwxrwxr-x 7 apache apache 4096 Jul 29 13:39 349e6026
drwxrwxrwx 5 root   root   4096 Jul 29 13:26 40473b7f
drwxrwxr-x 5 apache apache 4096 Jul 29 13:31 4939b2cb
drwxrwxr-x 3 apache apache 4096 Jul 29 13:39 55993b1e
drwxrwxrwx 7 root   root   4096 Jul 29 13:26 68e31155
drwxrwxrwx 5 root   root   4096 Jul 29 13:26 7007e07
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 770d3be4
drwxrwxrwx 3 root   root   4096 Jul 29 13:26 7ee37afb
drwxrwxrwx 3 root   root   4096 Jul 29 13:26 9e44a6d
drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a51215f4
drwxrwxr-x 2 apache apache 4096 Jul 29 13:31 a53efc6f
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 bfcc54b5
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 c8cb6423
drwxrwxrwx 2 root   root   4096 Jul 29 13:26 e006560
[root@aliyun assets]#

为什么yii2 要在 web/asset 目录下生成这样的缓存文件?(很多MVC都有这个趋势) 且生成的CSS JS缓存文件,并没有做minify,里面注释还是原样保留。 这些缓存目录文件,何时清除?是否是自动清除?

不会自动清除,为什么要生成这样的缓存文件?这个问题问的不对,因为这些不是缓存。

composer大行其道的时代,有很多人实现某一个特殊功能打包发布,开发者只要通过composer拿回来就可直接使用。

比如yii2-admin,它是一套yii权限管理的模块,自带了前端界面,他的资源放在自己的源代码目录下,当开发者使用时,yii将这个模块用到的前端资源发布到web/assets目录下,而不用开发者做其他额外的工作。

这些发布的资源是可以被压缩的,使用yii的 assets 命令,详情可以看这里assets

一.yii的资源包(assets)
Yii在资源包中管理资源,资源包简单的说就是放在一个目录下的资源集合, 当在视图中注册一个资源包,在渲染Web页面时会包含其中中的CSS和JavaScript文件。
二.定义资源包:
见assets/AppAsset.php文件,定义基础应用模板使用的主要资源包:

namespace app\assets; 
use yii\web\AssetBundle; 
class AppAsset extends AssetBundle { 
    public $basePath = '@webroot'; 
    public $baseUrl = '@web'; 
    public $css = [ 'css/site.css', ]; 
    public $js = [ ]; 
    public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ];
}

三.使用资源包

为使用资源包,在视图中调用[[yii\web\AssetBundle::register()]]方法先注册资源, 例如,在视图模板可使用如下代码注册资源包:

use app\assets\AppAsset; 
AppAsset::register($this); // $this 代表视图对象

如果在其他地方注册资源包,应提供视图对象,如在 小部件 类中注册资源包, 可以通过 $this->view 获取视图对象。

当在视图中注册一个资源包时,在背后Yii会注册它所依赖的资源包。
如果资源包是放在Web不可访问的目录下,会被发布到可访问的目录, 后续当视图渲染页面时,会生成这些注册包包含的CSS和JavaScript文件对应的 和