这里有新鲜出炉的精品教程,程序狗速度看过来!
Yii 2 完全根据 Yii 1.1 版本重写,后者是最流行的 PHP 框架之一。Yii 2 继承了 Yii 的简洁、快速、和高扩展性。Yii 2 需要 PHP 5.4,并且拥有现代 web 应用开发中最好的实例和协议。
这篇文章主要介绍了 Yii2 中使用 asset 压缩 js,CSS 文件的方法的相关资料, 需要的朋友可以参考下
官网文档
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 是方法名字
来源: http://www.phperz.com/article/17/0730/341025.html