php Laravel - 返回 json 和 http 状态码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31131159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:16:11  来源:igfitidea点击:

Laravel - Return json along with http status code

phpjsonlaravelhttp-status-codes

提问by Galivan

If I return an object:

如果我返回一个对象:

return Response::json([
    'hello' => $value
]);

the status code will be 200. How can I change it to 201, with a message and send it with the json object?.

状态代码将是 200。如何将其更改为 201,并带有消息并与 json 对象一起发送?。

I don't know if there is a way to just set the status code in Laravel.

我不知道有没有办法在 Laravel 中设置状态码。

回答by Tushar

You can use http_response_code()to set HTTP response code.

您可以使用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 将获取当前状态代码。如果您传递参数,它将设置响应代码。

http_response_code(201); // Set response status code to 201

For Laravel(Reference from: https://stackoverflow.com/a/14717895/2025923):

对于 Laravel(参考自:https: //stackoverflow.com/a/14717895/2025923 ):

return Response::json([
    'hello' => $value
], 201); // Status code here

回答by Jeremy C.

This is how I do it in Laravel 5

这就是我在 Laravel 5 中的做法

return Response::json(['hello' => $value],201);

Or using a helper function:

或者使用辅助函数:

return response()->json(['hello' => $value], 201); 

回答by TKoutsou

I think it is better practice to keep your response under single control and for this reason I found out the most official solution.

我认为最好的做法是将您的响应保持在单一控制下,因此我找到了最官方的解决方案。

response()->json([...])
    ->setStatusCode(Response::HTTP_OK, Response::$statusTexts[Response::HTTP_OK]);

add this after namespacedeclaration:

namespace声明后添加:

use Illuminate\Http\Response;

回答by iSensical

There are multiple ways

有多种方式

return \Response::json(['hello' => $value], STATUS_CODE);

return response()->json(['hello' => $value], STATUS_CODE);

where STATUS_CODE is your HTTP status code you want to send. Both are identical.

其中 STATUS_CODE 是您要发送的 HTTP 状态代码。两者是相同的。

if you are using Eloquent model, then simple return will also be auto converted in JSON by default like,

如果您使用的是 Eloquent 模型,那么默认情况下,简单返回也将自动转换为 JSON,例如,

return User::all();

回答by Mikayel Margaryan

return response(['title' => trans('web.errors.duplicate_title')], 422); //Unprocessable Entity

Hope my answer was helpful.

希望我的回答有帮助。

回答by Dylan Pierce

I prefer the response helper myself:

我自己更喜欢响应助手:

    return response()->json(['message' => 'Yup. This request succeeded.'], 200);

回答by Software Developer

It's better to do it with helper functionsrather than Facades. This solution will work well from Laravel 5.7 onwards

最好使用辅助函数而不是Facades 来完成。此解决方案从Laravel 5.7 开始运行良好

//import dependency
use Illuminate\Http\Response;

//snippet
return \response()->json([
   'status' => '403',//sample entry
   'message' => 'ACCOUNT ACTION HAS BEEN DISABLED',//sample message
], Response::HTTP_FORBIDDEN);//Illuminate\Http\Response sets appropriate headers