Issues with output buffering in CakePHP

Posted by Felix Geisendörfer, on Jul 16, 2006 - in PHP & CakePHP » Core & Hacking

Output buffering is a useful way to accomblish things in php like gzipping all html output and such. A while ago I wrote about it on this blog (see "A miracle called gzip") as well.

The reason I write about it again is, that I have discovered a little issue with it in CakePHP. Because if you use:

php
  1. ob_start ('ob_gzhandler')

or similar commands in your beforeFilter, you might suddenly notice that setting DEBUG > 1 in app/config/core.php will not give you the usal sql dump any longer. I'm not quite sure why this happens, but I think it's because CakePHP uses ob_buffering as well so there might be conflicts. The easiest solution I found is this one (this time as a complete code example):

php
  1. if (DEBUG<2)
  2. {            
  3.     @ob_start ('ob_gzhandler');
  4.     header('Content-type: text/html; charset: UTF-8');
  5.     header('Cache-Control: must-revalidate');
  6.     $offset = -1;
  7.     $ExpStr = "Expires: " .
  8.     gmdate('D, d M Y H:i:s',
  9.     time() + $offset) . ' GMT';
  10.     header($ExpStr);
  11. }

I had a hard time debugging this issue today, so I hope this might saves you the trouble I had ; ).

--Felix Geisendörfer aka the_undefined

Print this Post | Digg This | Stumble It | Delicious

2 Comments

[...] Update: I just found a little issue with this technique inside CakePHP. Read more about this here: Issues with output buffering in CakePHP [...]

PHPDeveloper.org on Jul 17, 2006:

ThinkingPHP: Issues with output buffering in CakePHP...

...

Add a comment