Laravel 不会遵守状态码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14704644/
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-09-14 07:32:37  来源:igfitidea点击:

Laravel won't obey status code

phpjsonlaravelhttp-status-codes

提问by Alex

I just can't understand, and don't know where else to look, as the response status code of the following code is always 200, even if I set it to 400 in the main Response class.

我只是无法理解,也不知道还能在哪里查看,因为以下代码的响应状态代码始终为 200,即使我在主响应类中将其设置为 400。

class Api_Controller extends Base_Controller
{

    public function __construct()
    {
          parent::__construct();

          //header("HTTP/1.0 404 Not Found"); ##> This works
          //die();

          $test = array('1' => '2');
          die(Response::json($test, 400));
    }

What am I missing? I'm not using any extended class, just the default...

我错过了什么?我没有使用任何扩展类,只是默认...

Update

更新

This is the output of the Response::json...above: http://pastebin.com/RGcinSdg

这是Response::json...上面的输出:http: //pastebin.com/RGcinSdg

As you can see, the output has the values that has been set... but still for some reason, returns 200

如您所见,输出具有已设置的值……但仍然出于某种原因,返回 200

Update2

更新2

The output of var_dump(http_response_code());is always 200

的输出var_dump(http_response_code());总是200

Update3 - Temporary fix

Update3 - 临时修复

I have activated an extended version of Response::jsonand add the following line to it

我已经激活了 的扩展版本Response::json并向其添加了以下行

http_response_code($status);

But I would still much like to know why doesn't it does it, the way it should

但我仍然很想知道它为什么不这样做,它应该采用的方式

回答by Oddman

You can't return responses from controller constructors - it just doesn't fit with the flow of the request lifecycle in Laravel.

你不能从控制器构造函数返回响应——它只是不符合 Laravel 中请求生命周期的流程。

There's two ways to do this. You can either:

有两种方法可以做到这一点。您可以:

a) Setup a response filter that handles whatever functionality it is you're trying to achieve or b) Force a controller ACTION to return a response. This would be done like so:

a) 设置一个响应过滤器来处理您试图实现的任何功能或 b) 强制控制器 ACTION 返回响应。这将像这样完成:

class Api_Controller extends Base_Controller
{
    public $restful = true;

    public function get_index()
    {
        return Response::json($test, 400);
    }
}

It DOES work - you're just doing it incorrectly :)

它确实有效 - 你只是做错了:)

回答by ecunado

The same problem happens if you forget the returnstatement:

如果您忘记return语句,也会发生同样的问题:

Response::json(array(
   'error' => true,
   'msg' => 'Bad request'
), 403);

instead of:

代替:

return Response::json(array(
   'error' => true,
   'msg' => 'Bad request'
), 403);

回答by Jannie Theunissen

Try the response()->json()syntax.

试试response()->json()语法。

So, for example, to flag validation error from a custom FormResquest, you can do this:

因此,例如,要从自定义 FormResquest 中标记验证错误,您可以执行以下操作:

/**
 * Get validation response for the request.
 *
 * @param  array $messages
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function response(array $messages)
{
    return response()->json($messages, 422);
}

回答by user2436802

To keep Google Webmaster Tools happy I detect and use the following in the header:

为了让 Google 网站管理员工具满意,我检测并在标题中使用以下内容:

META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"

回答by bstrahija

Controller methods always have to return responses. But I don't think you can return a response from the constructor. You would need to use a filter.

控制器方法总是必须返回响应。但我认为您不能从构造函数返回响应。您将需要使用过滤器。