齐乐娱乐网站

齐乐娱乐网站_yii2笔记:asset压缩js,css文件

  下面一起来学习一下PHP(http://www.ujiuye.com/zt/php/)知识之yii2笔记:asset压缩js,css文件

yii2笔记:asset压缩js,css文件_优就业

  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中对应代码即可

  更多PHP知识尽在优就业IT培训:http://www.ujiuye.com/zt/php/

Copyright © 2002-2011 DEDECMS. 织梦科技 版权所有   齐乐娱乐网站