_php instance of methods for compressing JS,CSS files using Asset in YII2

Source: Internet
Author: User
Tags yii

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.