Official website Documents
Http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
The Yii directory runs
Asset/template assets.php
Generates assets.php, which is a configuration template and modifies the following
<?php/** * Configuration file for the "Yii asset" console command. *//In the console environment, some path aliases could 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 {from}--js_output_file {to} ', ' Jscompressor ' => ' Java-jar Yuicompressor.jar--type js {from}-o {to} ',//Adjust com Mand/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 '], ' Basepat H ' => ' @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 are all used Yuicompressor
Then create the Js,css folder under Web/assets and set permissions 777
Install Java command line, under Ubuntu
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
assets-dev.php files are generated under Config
Add in Config/web.php's component configuration
' Assetmanager ' => [
' Bundles ' => require (__dir__. '/assets-'. Yii_env. '. php '),
],
F5 Refresh the page can see the use of compressed CSS and JS
If you want to cancel, comment out the corresponding code in the web.php
PS:YII2 controllers, method naming conventions, and access routes
If the module name or controller name or action name is in camel format, then each uppercase word in the route is connected with "-". Such as
Datetimecontroller::actionfastforward the corresponding route is Date-time/fast-forward.
For example: Http://dfms.com/backend/web/api-test/test-upload
Api-test is the controller name
Test-upload is the method name
The above is a small set to introduce the YII2 in the use of asset compressed Js,css file method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!