I am trying to let the user download a file in the Slim php framework.
The intended use is that the file will be:
http://api.test.com/item/123.json <- returns json string with data
http://api.test.com/item/123.pdf <- download pdf-file with human-readable presentation of data
I have the code producing the PDF, but what I need is to make Slim send the correct headers so the file will be downloaded.
This is the following code I have (working) for the existing system:
header("Pragma: public");
header('Content-disposition: attachment; filename='.$f->name);
header('Content-type: ' .$f->type);
header('Content-Transfer-Encoding: binary');
echo $f->data;
Following is my current (non-working) Slim-code where the headers I declare is not sent to the browser. Instead I get text/html. (Note that this example only contains one header, I have also tested to see if any other header manipulation would cause any effect, but it haven't). The switch-case of json/pdf/xml will be added later on.
R::setup();
$app = new \Slim\Slim();
$app->get('/item', function() use ($app) {
$f = R::load('file', 123);
$app->response->headers->set("Content-Type", "application/pdf"); //$f->type
$app->response->setBody($f->data);
});
$app->run();
The $app->response->setBody($f->data) however works fine.