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!
Pingback: Changing Content Type with Slim Framework | Adv...
I’ve been playing with Slim lately and I’ve encountered that the documentation (http://docs.slimframework.com) it’s the best resource, maybe is lacking of something on structuring applications, but on the Slim’s API it has everything cover.
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
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.
Thanks!!
–Donnie
Thanks for clearing it out. I couldn’t find any correct information about how to set headers in Slim for file download.