Compress CSS or JS files in only one file to increase performance and decrease request of many files. This extension (version > 1.3) use CssMin and JavaScriptPacker to compress.
assetManager (native)
main.php 'components'=>array(
... 'dynamicRes'=>array( 'class' => 'application.extensions.DynamicRes.DynamicRes', 'urlConfig' => array( // Its fix Css, and convert Url to RealName 'baseUrl' => '/', // Url of your Site (ending with /), modify it if you use subdomain 'basePath' => dirname(__FILE__).'/../../', // path of your site (ending with /) (No Change This) ) ), ...
In your layouts puts
...
<head>
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/style.css'); ?>
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/reset.css'); ?>
<?php Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/common.css'); ?>
</head>
...
</html>
...
<?php Yii::app()->dynamicRes->saveScheme(); // use This function on end of layout or use on afterRender of your Controller Class (Auto SaveScheme) ?>
Remember, Do not save scheme again in your views or other files
to facilitate, you can call the function saveScheme on function "afterRender" of controller. Example:
components/controller.php
<?php
class Controller extends CController {
protected function afterRender($view, &$output) {
Yii::app()->dynamicRes->saveScheme();
}
}
All files are placed in your asset folder, to cache use htaccess. (do not worry about the name of the files, they are switched when the original file is modified).
Example of cache:
.htacces
<FilesMatch "\.(js|css)$">
Header set Expires "Sun, 15 Feb 2020 20:00:00 GMT"
</FilesMatch>
or
<ifModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </ifModule>
or
<ifModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 seconds" ExpiresByType text/html "access plus 1 seconds" ExpiresByType image/gif "access plus 2592000 seconds" ExpiresByType image/jpeg "access plus 2592000 seconds" ExpiresByType image/png "access plus 2592000 seconds" ExpiresByType text/css "access plus 604800 seconds" ExpiresByType text/javascript "access plus 216000 seconds" ExpiresByType application/x-javascript "access plus 216000 seconds" </ifModule>
Download: Click Here To Download Extension
Total 20 comments
here is my modification.
Wow, this extension has sped up my site (+1). Thanks for sharing it.
Can I also use this extension to compress these JavaScript files used by Yii and extensions?
For example: jquery.min.js, jquery.yii.js, query.yiigridview.js, etc.
Thanks, Jay.
now it works correctly
i always had problems with other cahce extensions because path and script errors on minify because a use of massive scripts ... but that works correctly
thanks and good job :)
just a resquest: minify inline js and css processed by Yii eg. Yii->app()->clientScript->javascript(); and ->css
Bug Fixed,
now, baseUrl config is absolete, use only:
css:
RESULT:
also happen in more situations
You need to config Url Base of Your Site, your baseUrl is "/yii/escorifa/"
thanks i now see where its the error
but why it repeat path? if im using yii base url
Upgrade to "DynamicRes 1.5" and use function debug();
use this after include yours files (in your view)
i dont know why, but it still not working ....
all placed with savescheme but i got no results :(
my code:
change config to
note: basePath and baseUrl end with "/"
hi,
i use it in controller like it say on setup
so i have to useit in view instead controller?
Everything works fine. Will let you know how it works with big css/js files after some tests.
use "Yii::app()->dynamicRes->saveScheme();" after yours "Yii::app()->dynamicRes->registerCssFile(Yii::app()->request->baseUrl . '/css/style.css')"
Version 1.4 > CssMin Bug Fix 4 and Fix Bug in double "saveScheme"
Hi,
i have installed this, and follow all setups but dont work for me in view page source, its blank of scripts or css includes
Thanks
I found bug in registerScriptFile(), but it's already fixed. Nice. :) But there is something wrong with CssMin - maybe because my example was for CssMin 2.0.1 and you are using CssMin 3.0 (didn't know it is out), so entire script breakes. You have to change line 106 to this:
BTW have you ever tested JavaScriptPacker for large files? I've tried a lot of libraries and some of them (JsMin+) caused memory leak while processing big files. Don't have time to try this now, maybe someone else will test.
Fixed bug in "registerScriptFile"
compress of css is using "CssMin" compress of js is using "JavaScriptPacker"
I will test it tomorrow and see if there are any other bugs. :)
About CssMin - it is really simple.
As you see you only need to include CssMin file and call CssMin::minify($code, $options) function. You may want to check out $options, but that's all.
Thanks, bug in parserCss is fixed now
I will study this CssMin to replace compress
Hello again. :) It seems that you have one more bug (or more, we will see :) ) - in parserCss() function, line 37. Everything is ok if you use relative paths. Problem shows when you use absolute paths, for example:
Line 37 changes it to something like that (/css is my original folder for css files):
Also you should consider replacing compressContent() with something better. CssMin is great class. JS compressing would be nice too - Vince developed nice extension, google-complier-extension. Think about this. ;)
Thanks, the bug was fixed
Leave a comment
Please login to leave your comment.