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 m.joind.in. 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!