The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Enable Gzip compression

slouge's Avatar


11 Apr, 2012 06:56 PM

I'm trying to enable compression with slim.
How do that ?
I'm on 1and1 host shared

  1. Support Staff 1 Posted by Josh Lockhart on 11 Apr, 2012 07:03 PM

    Josh Lockhart's Avatar

    This is typically something you do in your web server virtual host configuration. With Apache and mod_deflate you would add this to the virtual host config or to a .htaccess file:

    AddOutputFilterByType DEFLATE text/html

    I'm not sure if 1and1 has the deflate module installed though. You'll have to ask 1and1 about that.

  2. 2 Posted by raphael on 19 Apr, 2012 09:48 AM

    raphael's Avatar

    Like this maybe ?

    $app->get('/',function() use ($app){
        // -> slim view (?)
        $buffer = ob_get_contents();
        // output
        $response = $app->response();
        if(strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')!==FALSE){ // don't know if this is inside the $response object, or environment ...
            $buffer = gzencode($buffer);
            $response['Content-Type'] = 'text/html';
            $response['Content-Encoding'] = 'gzip';

    @Josh: Couldn't you make a $app->response->body(function(){},$gzip_bool); method for that kind of thing? Like, if body != string and == callable while second param == true anything between the anonymous function will be gzipped?

    Just an idea. Didn't check if this is possible …

  3. Support Staff 3 Posted by Josh Lockhart on 19 Apr, 2012 11:52 AM

    Josh Lockhart's Avatar

    It's certainly possible to gzip encode text in Slim before it is handed back to the web server. But it's much easier to have the server do this for you. With some hosts, the server will do this automatically.

    This is an ideal candidate for custom middleware. If you want to write one, I can add it to the Slim-Extras repo on GitHub.

  4. 4 Posted by raphael on 24 Apr, 2012 07:02 PM

    raphael's Avatar

    Will send it in ASAP. Probably nice to have something like that …

  5. Support Staff 5 Posted by Josh Lockhart on 24 Apr, 2012 08:52 PM

    Josh Lockhart's Avatar

    When you write your middleware, make sure you check that the response body is a string before making any changes to the response. Just a heads up for some changes coming down the pipe :)

  6. 6 Posted by raphael on 24 Apr, 2012 10:00 PM

    raphael's Avatar

    OK will do …

  7. Andrew Smith closed this discussion on 02 Aug, 2012 10:58 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac