Yii2中使用asset压缩js,css文件的方法
作者:zergling9999 时间:2024-05-02 17:16:13
官网文档
http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
yii目录下运行
asset/template assets.php
生成assets.php,这是一个配置模板,并修改如下
<?php
/**
* Configuration file for the "yii asset" console command.
*/
// In the console environment, some path aliases may not exist. Please define these:
Yii::setAlias('@webroot', __DIR__ . '/web');
Yii::setAlias('@web', '/');
return [
// Adjust command/callback for JavaScript files compressing:
// 'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
'jsCompressor' => 'java -jar yuicompressor.jar --type js {from} -o {to}',
// Adjust command/callback for CSS files compressing:
'cssCompressor' => 'java -jar yuicompressor.jar --type css {from} -o {to}',
// The list of asset bundles to compress:
'bundles' => [
'app\assets\AppAsset',
'yii\web\YiiAsset',
'yii\web\JqueryAsset',
],
// Asset bundle for compression output:
'targets' => [
'all' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
'js' => 'js/all-{hash}.js',
'css' => 'css/all-{hash}.css',
],
],
// Asset manager configuration:
'assetManager' => [
'basePath' => '@webroot/assets',
'baseUrl' => '@web/assets',
],
];
这里css和js全部用yuicompressor了
然后在web/assets下创建js,css文件夹,并设置权限777
安装java命令行,ubuntu下
sudo apt-get install default-jre
下载yuicompressor.jar:
https://github.com/yui/yuicompressor/releases
放到yii根目录下
运行
./yii asset assets.php config/assets-dev.php
在config下会生成assets-dev.php文件
在config/web.php的component配置中添加
'assetManager' => [
'bundles' => require(__DIR__ . '/assets-' . YII_ENV . '.php'),
],
F5刷新页面就可以看到使用了压缩后的css和js了
如果要取消,注释掉web.php中对应代码即可
PS:yii2 控制器、方法命名规范和访问路由
如果模块名称或者控制器名称或者动作名称是用的骆驼格式的命名写法,那么路由里面的每个大写单词之间都要用“-”来连接。如
DateTimeController::actionFastForward 相应的路由为 date-time/fast-forward。
例如:http://dfms.com/backend/web/api-test/test-upload
api-test是控制器名字
test-upload是方法名字
以上所述是小编给大家介绍的Yii2中使用asset压缩js,css文件的方法网站的支持!
来源:http://www.cnblogs.com/zergling9999/archive/2016/11/24/6097783.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Access 导入到SQL Server 2005的方法小结
用python爬取分析淘宝商品信息详解技术篇
![](https://img.aspxhome.com/file/2023/3/90933_0s.jpg)
MySQL存储过程例子(包含事务,输出参数,嵌套调用)
Python实现仿射密码的思路详解
![](https://img.aspxhome.com/file/2023/4/115684_0s.png)
PJBlog3优化——单击自动输入验证码
python数据分析之线性回归选择基金
![](https://img.aspxhome.com/file/2023/7/69577_0s.webp)
如何把数据库记录显示到列表框里去?
Python使用django搭建web开发环境
![](https://img.aspxhome.com/file/2023/9/90699_0s.jpg)
Python中人脸图像特征提取方法(HOG、Dlib、CNN)简述
![](https://img.aspxhome.com/file/2023/7/101457_0s.png)
Go 库性能分析工具pprof
详谈python3中用for循环删除列表中元素的坑
用非动态SQL Server SQL语句来对动态查询进行执行
Python找出最小的K个数实例代码
原生js实现Flappy Bird小游戏
![](https://img.aspxhome.com/file/2023/4/132634_0s.jpg)
JS中window.open全屏命令解析及使用示例
opencv模板匹配相同位置去除重复的框
![](https://img.aspxhome.com/file/2023/0/127780_0s.jpg)
SpringSecurity从数据库中获取用户信息进行验证的案例详解
![](https://img.aspxhome.com/file/2023/8/122288_0s.png)
python操作mysql代码总结
Python实现聚类K-means算法详解
![](https://img.aspxhome.com/file/2023/2/105812_0s.png)
为什么mysql自增主键不是连续的
![](https://img.aspxhome.com/file/2023/9/124869_0s.png)