Home  >  Article  >  Backend Development  >  Method of using asset to compress js and css files in Yii2_php example

Method of using asset to compress js and css files in Yii2_php example

WBOY
WBOYOriginal
2016-12-05 13:28:281278browse

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, which is a configuration template, and modify it as follows

<&#63;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',
],
];

The css and js here all use yuicompressor

Then create js and css folders under web/assets and set permissions to 777

Install java command line, under ubuntu

sudo apt-get install default-jre

Download yuicompressor.jar:

https://github.com/yui/yuicompressor/releases

Place it in the yii root directory

Run

./yii asset assets.php config/assets-dev.php

The assets-dev.php file will be generated under config

Add in the component configuration of config/web.php

'assetManager' => [
'bundles' => require(__DIR__ . '/assets-' . YII_ENV . '.php'),
],

Refresh the page with F5 and you can see the compressed css and js

If you want to cancel, just comment out the corresponding code in web.php

PS: yii2 controller, method naming convention and access routing

If the module name or controller name or action name is written in camel format, then each uppercase word in the route must be 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 the method of using assets to compress js and css files in Yii2 introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for your support of the Script House website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn