php – Laravel – 返回json以及http状态代码

如果我返回一个对象:

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...