Search code examples
yiiyii-componentsyii-events

YII compress your application output using gzip


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


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.