YII compress your application output using gzip

蹲街弑〆低调 提交于 2019-12-04 16:30:25

The above code buffers the content and gzips it according to browser, rather than sending it straight away.

Yii::app()->onBeginRequest = function($event)
{
return ob_start("ob_gzhandler");
};

The above means that when the requests starts, it will buffer the content, and using the callback will set the content as gzip,deflate or none, depending on browser.

Yii::app()->onEndRequest = function($event)
{
return ob_end_flush();
};

The above code simply means that at the end of the request, it will output the buffer contents.

It buffers the content, and just before sending it the browser, asks if the browser can accept zipped content. If it can, it will zip the HTML before supplying. Otherwise, it will supply it unzipped.

Zipped content reduces the size of the HTML the browser needs to download, which can increase performance. How much performance gain your users will see depends on the size of the HTML - bigger pages will see more benefit, while tiny pages may actually take longer to render, because the browser has to unzip the content first. Use Firebug or Chrome Developer Toolbars to see whether it's worth it.

Also, check the impact on the server side. Again, the downside of increased server load can outweigh the increased client-side page render speed. Hence, it works best with lots of caching.

This is normally something you do when you are optimising the site, looking for performance gains.

if you want add gzhanlder straight to main config file you Can Set Following lines in main.php

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

this Two lines Add GzipHandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!