Difference between #6 and #5 of Enabling Gzip

unchanged
Title
Enabling Gzip
unchanged
Category
Tutorials
unchanged
Tags
changed
Content
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.

~~~
[php]
'onBeginRequest'=>create_function('$event', 'return
ob_start("ob_gzhandler");'),
'onEndRequest'=>create_function('$event', 'return ob_end_flush();'),
~~~

An overview about this:

* PHP's `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.
* Indexing these items as `onBeginRequest` and `onEndRequest` will trigger
attaching callbacks to application events.
* Event handlers are even acceptable since they'll take the event object as
first argument.
* Please visit official PHP site for more information regarding
[ob_start](http://php.net/ob_start) and
[ob_gzhandler](http://php.net/ob_gzhandler). In summary, the latter will check
whether the client is compatible with gzipped content, set the Content-Encoding
header and compress all the content generated during processRequest accordingly.
* If the current browser is not capable of uncompressing gzip, gzhandler will
leave the output untouched.

(If you need additional functionality, you may want to create a separate file
for these functions.)

### Links
[Chinese
version](http://dreamneverfall.cn/node/109)version](http://projects.ourplanet.tk/node/109)
Write new article
  • Written by: pestaa
  • Updated by: Yang He
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +6 / -1
  • Viewed: 18,169 times
  • Created on: Jul 10, 2009
  • Last updated: Jun 29, 2012