Reputation: 8880
I discovered Slim yesterday and really like it. I have run into some minor issues. Here is one:
I would like to send out extra headers from my jQuery UI app to my Slim REST API. Not a problem at the jQuery end- $.ajax provides that capability. However, I thought I would write up a small Slim app to test out Slim's own ability to give me access to all request headers. Here is that app
function indexFunction()
{
global $app;
$headers = $app->request->headers;
echo json_encode($headers);
}
header('Content-type:text/plain');
$app = new \Slim\Slim();
$app->get("/",'indexFunction');
$app->run();
I opened DHC in Chrome and fired off a GET request
http://ipaddr/slimrestapi
after adding the header xhash = abc123
For good measure I started up Fiddler and watched the traffic as I sent out that request. Fiddler faithfully reported the following headers
Host: ipaddr
Connection: keep-alive
xhash: abc123
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,de;q=0.6,fr;q=0.4
However, the results echoed back by slim are an empty JSON object, {}.
Am I misunderstanding something here or is there a bug in Slim? I'd much appreciate any help.
Upvotes: 2
Views: 9785
Reputation: 2511
The headers in Slim are an instance of Slim\Helper\Set
You can get the content as you want with the all()
function
json_encode($app->request()->headers()->all());
Or in a full example
$app->get('/', function() use ($app) {
echo json_encode($app->request()->headers()->all());
echo $app->request()->headers()->get('xhash');
});
The example also shows how you can avoid using that global
statement. This is a good read on that subject http://tomnomnom.com/posts/why-global-state-is-the-devil-and-how-to-avoid-using-it
Upvotes: 6