slimphp-api / slim-api

Command line interface for generating API end points with Slim
MIT License
11 stars 1 forks source link

json middleware #17

Closed gabriel403 closed 8 years ago

gabriel403 commented 9 years ago
$this->registerMediaTypeParser('application/json', function ($input) {
    return json_decode($input, true);
});
$acceptHeaders = [];
if (count($request->getHeader('ACCEPT')) > 0) {
    foreach ($request->getHeader('ACCEPT') as $header) {
        $acceptHeaders = array_merge($acceptHeaders, explode(', ', $header));
    }
}

if (in_array('application/json', $acceptHeaders)) {
    $response = $response->withJson($response->getBody());
}

return $response;
gabriel403 commented 9 years ago

Or possibly $request->getMediaType() rather than accept headers?

gabriel403 commented 9 years ago

First part now covered in slim core slimphp/Slim#1489