Use asset to compress js and css files in Yii2, and yii2asset
Official Website documentation
Http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
Run in the yii directory
Asset/template assets. php
Generate assets. php. This is a configuration template and modify it as follows:
<?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',],];
Here, css and js all use yuicompressor.
Create the js and css folders under web/assets and set the permissions to 777.
Install the java command line.
Sudo apt-get install default-jre
Download yuicompressor. jar:
Https://github.com/yui/yuicompressor/releases
Put it in the yii root directory
Run
../Yii asset assets. php config/assets-dev.php
A assets-dev.php file is generated under config
Add
'assetManager' => ['bundles' => require(__DIR__ . '/assets-' . YII_ENV . '.php'),],
When F5 refreshes the page, we can see that the compressed css and js are used.
If you want to cancel, just comment out the corresponding code in web. php.
PS: yii2 controller, method naming rules, and access Routing
If the module name, controller name, or action name is written in the camel format, "-" is used to connect each uppercase word in the route. For example
DateTimeController: The corresponding route of actionFastForward is date-time/fast-forward.
Example: http://dfms.com/backend/web/api-test/test-upload
Api-test is the Controller name
Test-upload is the method name.
The above section describes how to use asset to compress js and css files in Yii2. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!