【问题标题】:Send custom 500 http response from Laravel从 Laravel 发送自定义 500 http 响应
【发布时间】:2019-05-01 11:05:48
【问题描述】:

我想发送一个状态为 500 的自定义 http 响应。 尝试了以下代码,但它返回的状态为 200。

return response()
            ->json([
                'code'      =>  500,
                 'message'   =>  'custom error'
            ], 500);

如何从 laravel 控制器发送 http 状态 500?

【问题讨论】:

标签: laravel laravel-5


【解决方案1】:

试试这个。

return abort(500, 'custom error');

更多信息:

https://laravel.com/docs/5.7/errors#http-exceptions

希望对你有帮助

更新

那就试试这个

return response()->json(['message' => 'error message'], 500);

这将发送 500 状态码而不是 200

更多信息:

https://laravel.com/api/5.7/Illuminate/Contracts/Routing/ResponseFactory.html#method_json

希望对您有所帮助。

【讨论】:

  • AFAIK,abort 实际上呈现刀片模板,而不是 JSON,这对 OP 有用吗?
  • 我相信你需要调用response作为一个函数:return response()->...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多