ラボ > Laravel、Lumen:ajax、api絡み
lalavel・API responseにHTTPステータスコードを追加して返す
リクエストにあわせて405とか色々ステータスコードを変更して返したい。
作成日:2019-06-05, 更新日:2019-06-05
ファザードを使う場合
$response_code = 405; $response = array( 〇〇〇 => 〇〇〇, 〇〇〇 => 〇〇〇, 〇〇〇 => 〇〇〇, ); $response_body = \Response::json($response); return response($response_body, $response_code) ->header('Cache-Control', 'no-cache') ->header('Content-Type', 'application/json') ->header('Content-Length', strlen($response_body));
ファザードを使わない場合
$response_code = 405; $response = array( 〇〇〇 => 〇〇〇, 〇〇〇 => 〇〇〇, 〇〇〇 => 〇〇〇, ); $response_body = json_encode($response); http_response_code($response_code); header('Cache-Control: no-cache'); header('Content-Type: application/json'); header('Content-Length: ' . strlen($response_body) ); echo $response_body; exit;
※「exit」は必須