How to enable #Gzip compression in Yii?


Updated November 23, 2013 ‚óŹ 2,380 views

Enabling Gzip compression in delivering your webpage will improve your sites performance. If you used Yii PHP Framework in developing your website, you only need two lines of code to enable Gzip compression.

In you main.php configuration file, just add the following code:

//GZIP compress   
'onBeginRequest'=>create_function('$event', 'return ob_start("ob_gzhandler");'),
'onEndRequest'=>create_function('$event', 'return ob_end_flush();'),

Now, if you are using Google's PageSpeed Insight to get optimization suggestion for your website, it might still be saying that your page needs to enable compression. As per Google's PageSpeed Insights FAQ:

PageSpeed Insights' results are based on headers that were actually returned to your client, so if you are running the analysis on a client machine that is using such anti-virus software, or that sits behind an intermediate proxy server (many proxies are transparent, and you may not even be aware of a proxy intervening between your client and web server), they may be the cause of this issue.

You can use PageSpeed Insights Chrome extension to verify as suggested from PageSpeed Insights FAQ.

1 Comments

0

Awesome. Yii is the best PHP framwork out there. It's very easy to implement optimizations like this. Thanks!

master_yii · 6 years ago
permalink · reply (0)

Our Terms of Use and Privacy Policy have changed.
By continuing to use this site, you are agreeing to the new Privacy Policy and Terms of Service.