问题
How do I set a JSON header in Slim 3?
$app->get('/joinable', function ($request, $response, $args) {
header('Content-Type: application/json');
return getJoinable(); // Returns JSON_encoded data
});
I have tried the following
$response = $app->response();
$response['Content-Type'] = 'application/json';
$app->contentType('application/json');
回答1:
Never used Slim framework, but according to their documentation, it should be something in the lines of:
$app->get('/joinable', function ($request, $response, $args) {
$body = $response->getBody();
$body->write('{"your_content": "here"}');
return $response->withHeader(
'Content-Type',
'application/json'
)->withBody($body);
});
What you are trying with header('Content-Type: application/json');
may actually work, but since you are using a framework for your application, you should respect their guidelines, or you'll end up with lots of problems. Also, that getJoinable()
is a global call, you should really learn some OOP and, more than that, follow the PSR guidelines, because Slim 3 is built using those guidelines.
回答2:
You can simply do the following (note I am using the built in json encoder helper):
return $response->withJson($dataArray)->withHeader('Content-Type', 'application/json');
if you are returning a lot of JSON, you might consider creating a route group:
$app->group('/api', function () {
$this->response->withHeader('Content-Type', 'application/json');
$this->get(...);
$this->get(...);
}
It's a time saver and keeps your code clean scaleable and maintainable.
回答3:
The request object has a method to convert an array to JSON and set the header at the same time. See documentation here
$app->get('/joinable', function ($request, $response, $args) {
return $response->withJson(getJoinable());
});
Except that getJoinable()
needs to return an Array, because withJson()
will convert it to json for you.
Now, if you insist on setting the header yourself, see the documentation here
The code would look like this
$app->get('/joinable', function ($request, $response, $args) {
$body = $this->getBody();
$body->rewind(); // ensure your JSON is the only thing in the body
$body->write(getJoinable());
return $response->withHeader('Content-Type', 'application/json;charset=utf-8');
});
If you're sure the body is empty at this point, you can save a step and just use the write()
method in the Response
object.
$app->get('/joinable', function ($request, $response, $args) {
$response->write(getJoinable());
$response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
return $response;
});
And even shorter notation
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(getJoinable())
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
With PHP 5.4+ you may also want to a json pretty printing (not suggested for larger payloads, as it adds around 10-25% more to your transferred bytesize):
$app->get('/joinable', function ($request, $response, $args) {
return $response->write(json_encode(getJoinable(), JSON_PRETTY_PRINT))
->withHeader('Content-Type', 'application/json;charset=utf-8');
});
回答4:
First, I would advice a total abstraction of the renderer. One that would be easier to maintain at the long run. You could have a Renderer class with this method for instance:
public function render(Response $response, array $data, $status_code)
{
return $response->withStatus((int) $status_code)
->withHeader('Content-Type', 'application/json;charset=utf-8')
->withJson($data);
}
where $response
is an instance of Slim\Http\Response
.
You can find a more robust Renderer class here: slim3-api-output-format
Hope this helps someone else.
来源:https://stackoverflow.com/questions/35439409/how-do-i-set-a-json-header-in-slim-3-framework-php