Question

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 ??

Était-ce utile?

La solution

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top