Tuesday, July 3, 2012

how to define and use handlers in YII

Find more on http://Learnyii.ebharat.org

handlers, let's review some real life examples
as follows:
It is common practice to compress your application output using gzip to save client
bandwidth and speed up page loading time. If you have an access to fine-tune your
server, then you can instruct it to do so, but in some environments such as shared
hosting, you can't.
Fortunately, PHP can gzip the application output using output buffering and
ob_gzhandler. In order to do so, we should start buffering the output when
the application starts and releases the gzipped output, when it finishes.
Yii's application component has two events that will come in handy in this case:
CApplication::onBeginRequest and CApplication::onEndRequest. Let's
use them. Put the following in index.php after configuring an application but before
running it:

require_once($yii);
$app = Yii::createWebApplication($config);
// attaching a handler to application start
Yii::app()->onBeginRequest = function($event)
{
// starting output buffering with gzip handler
return ob_start("ob_gzhandler");
};
// attaching a handler to application end
Yii::app()->onEndRequest = function($event)
{
// releasing output buffer
return ob_end_flush();
};
$app->run();

No comments:

Post a Comment