Use asset to compress js and css files in Yii2, and yii2asset

Source: Internet
Author: User

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!

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.