响应 header 重复(流明)

Response header duplicated (Lumen)

续:

在 Lumen 中使用以下异常处理程序时,X-Powered-By header 会重复,即使 $replaceheader() 方法的第三个参数)默认为true(即使手动设置,如下所示,也不起作用)。

public function render($request, Exception $e)
{
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
    {
        return response(view('not-found'), 404)->header('X-Powered-By', env('APP_NAME')."/".env('APP_VER'), true);
    }

    return parent::render($e);
}

响应header:

HTTP/1.0 404 Not Found
Date: Sat, 23 May 2015 08:05:13 GMT
Server: Apache
X-Powered-By: PHP/5.6.3
Cache-Control: no-cache
X-Powered-By: AppName/1.0.0
Connection: close
Content-Type: text/html; charset=UTF-8

唯一有效的方法是在调用 ->header 之前使用 header_remove('X-Powered-By')。由于相应地设置了 $replace 参数,我不必这样做。

是否有更好的方法来防止 X-Powered-By header 重复?

设置

expose_php = Off

在您的php.ini中删除

X-Powered-By: PHP/5.6.3

expose_php

我无法让它与方法链一起工作,但是,如果我这样做:

header('X-Powered-By: '.env('APP_NAME')."/".env('APP_VER'));
return response(view('not-found'), 404);

...如您所愿。但请注意,根据 PHP 手册,header 中只有一个参数:

void header ( string $string [, bool $replace = true [, int $http_response_code ]] )

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.

Source: http://php.net/manual/en/function.header.php

...意思是,它不是像 str_replace 这样的 "replace this with that" 类型的项目。如果您在第一个参数中输入的字符串与另一个 header 项目相似,它将自动替换为您输入的任何内容。

旁注:我还尝试将响应函数的第三个参数设置为包含 X-Powered-By header 的数组,但无济于事。