如果我返回一个对象:
return Response::json([
'hello' => $value
]);
状态码将是 200。如何将其更改为 201,并带有消息并与 json 对象一起发送?
我不知道有没有办法只在 Laravel 中设置状态码。
最佳答案
您可以使用 http_response_code()
设置 HTTP 响应代码。
If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code.
http_response_code(201); // Set response status code to 201
对于 Laravel(引用自:https://stackoverflow.com/a/14717895/2025923):
return Response::json([
'hello' => $value
], 201); // Status code here
关于php - Laravel - 返回 json 和 http 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131159/