Adding HTTP compression to your application could never have been easier!
Just insert the following two lines to your configuration array, and PHP will do the rest.
'onBeginRequest'=>create_function('$event', 'return ob_start("ob_gzhandler");'), 'onEndRequest'=>create_function('$event', 'return ob_end_flush();'),
An overview about this:
create_function will create lambda functions in runtime, so you don't have to worry about external files. It'll take the parameter list and the internal code, respectively, and return the newly added function name, which will serve here as callbacks.onBeginRequest and onEndRequest will trigger attaching callbacks to application events.(If you need additional functionality, you may want to create a separate file for these functions.)
Total 5 comments
Please note that the PHP manual says:
You cannot use both ob_gzhandler() and zlib.output_compression. Also note that using zlib.output_compression is preferred over ob_gzhandler().
Thanks :)
Hi ScallioXTX and others,
I also had the compression problem when the application had to display an error with the action site/error from the errorHandler.
I looked at the file framework/base/CErrorHandler.php and found the $discardOutput property that does an ob_end_clean(), I don't really understand what this php function does in this case, I sopose it clears all the previous buffer before sending the new error page.
Well if you set discardOutput to false from the config it will not give you the compression problem anymore:
Hope it helps someone!
<?php class WebBehavior extends CBehavior{ public function events(){ return array( 'onBeginRequest'=>'beginRequest', 'onEndRequest'=>'endRequest' ); }
public function beginRequest(){ return ob_start("ob_gzhandler"); } public function endRequest(){ return ob_end_flush(); }}
then in index.php modify this line as Yii::createWebApplication($config)->run();
$app = Yii::createWebApplication($config); $app->attachBehavior('WebBehavior','application.behavior.WebBehavior'); $app->run();
When using this method, Firefox gives the following error when an exception is raised:
"The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression."
The problem occurs because CErrorHandler closes all output buffers, so the output is no longer gzipped, but the ob_start('ob_gzhandler') in onApplicationBegin has already sent the header that page is gzip compressed. So you get the situation that the HTTP headers tell firefox that the content is gzipped, while in fact it is not.
This can be resolved by setting CErrorHandler::errorAction to something like "site/error" and then implement this action in the SiteController class.
In the config:
return array( ... 'components' => array( 'errorHandler' => array( 'errorAction' => 'site/error' ... ) ) );In the SiteController:
public function actionError() { ob_start('ob_gzhandler'); if(Yii::app()->errorHandler->error) { $error = Yii::app()->errorHandler->error; } // handle the error here }We call ob_start('ob_gzhandler') here again so the content is gzipped, which solves the problem.
An alternative approach (not tested, but should work theoretically) is to extend the CErrorHandler class like so
class MyErrorHandler extends CErrorHandler { protected function render($view,$data) { ob_start('ob_gzhandler'); parent::render($view,$data); } }and in the config:
return array( ... 'components' => array( 'errorHandler' => array( 'class' => 'path.to.MyErrorHandler' ... ) ) );Leave a comment
Please login to leave your comment.