YII compress your application output using gzip
Asked Answered
R

3

3

what is the benefit of below code that is two events.

what its actually doing ??

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

Yii::app()->onEndRequest = function($event)
{

return ob_end_flush();
};

$app->run();

please explain the function of this code in my application.what it does ?? and how can it help me ??

Revocable answered 11/6, 2012 at 7:20 Comment(0)
C
4

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.

Circumcise answered 11/6, 2012 at 9:28 Comment(3)
on Client side how its beneficial ??Revocable
Content is gzipped if possible, so less data to download, meaning a faster page speed.Circumcise
Here's some common site performance guidelines which should help. developer.yahoo.com/performance/rules.html you can scroll down to the gzip section.Circumcise
D
2

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.

Dovetail answered 11/6, 2012 at 9:41 Comment(0)
C
1

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

Cryptonym answered 4/3, 2013 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.