この記事の内容はすでに修正されています
slimからjsonでエラーレスポンスを返そうとして
return $response->withStatus(500)->withJson([]);
ってやったらレスポンスのステータスコードが200で返ってきた
withJson
の実装読んでみると
public function withJson($data, $status = 200, $encodingOptions = 0)
{
$body = $this->getBody();
$body->rewind();
$body->write(json_encode($data, $encodingOptions));
return $this->withStatus($status)->withHeader('Content-Type', 'application/json;charset=utf-8');
}
現在のステータスコードに関係なく200で上書きするので注意
第二引数でステータスコード指定するかwithJson
した後にwithStatus
しましょう
$response->withStatus(500)->withJson([]);
と
$response->withJson([])->withStatus(500);
でレスポンス変わっちゃうのはどうなんだろう
追記
https://github.com/slimphp/Slim/pull/1737
pull req送ってみたものの議論が始まって英語力が足りないのでついていけてないけど3.2.0で取り込んでもらえそう?