Changing Content Type with Slim Framework

Slim framework has recently invaded my life, I picked it up for a hobby project, recommended it to a client who then contracted me to do quite a lot more development, and it’s also used for One thing that has tripped me up a couple of times is how to return not-HTML from Slim as it just bins any headers you try to send yourself. I think also that the “right” way to do this may have changed between versions as I also found some examples that didn’t work! What did work for me was this:

        $response = $app->response();
        $response->header("Content-Type", "text/javascript");

The $app variable is the Slim\Slim instance for your application, once you have that, you can just add on any headers you need to with this call to header(). It wasn’t obvious to me and there weren’t a lot of resources for this, so I thought I’d share!

5 thoughts on “Changing Content Type with Slim Framework

  1. Pingback: Changing Content Type with Slim Framework | Adv...

  2. Hello,

    I’m starting to use SLIM and it’s a great framework, glad it’s one you also use, it must mean that it’s that great.

    Love reading you blog, please do keep sharing tips with us.

    Regards from Portugal

  3. I hadn’t used PHP in 10 years and then, all of a sudden, I found myself trying to add headers to the response of an API server that’s written entirely in PHP. At first I was stumped because the documentation for Slim seemed incorrect. However, one quick Google search turned up this article, which probably saved me an hour of work.



Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.