调用未定义的方法Symfony \ Component \ HttpFoundation \ Response :: header()

时间:2017-04-24 16:28:50

标签: php laravel cors laravel-5.4 laravel-passport

您好我正在使用一个cors中间件似乎工作正常,直到我添加Laravel Passport现在它有问题..它显示错误

 Call to undefined method Symfony\Component\HttpFoundation\Response::header() on line number 36 

这是我的中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Response;

class Cors
{

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

// ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers' => "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers"
        ];


        if ($request->getMethod() == "OPTIONS") {
// The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);
        foreach ($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }

}

问题出在if条件之后..任何帮助都会被感染

3 个答案:

答案 0 :(得分:19)

嗨,我遇到了同样的问题。看起来这是Passport中的错误,并且有许多开发人员处于相同的情况。我刚刚找到了解决这个问题的方法。我们得到此错误的原因是因为我们在middleware中获得的响应对象通常是Illuminate\Http\Response类的实例,我们可以使用方法header('Header-Key', 'Header-Value')设置响应头,而请求由Passport处理将是Symfony\Component\HttpFoundation\Response的一个实例,这就是我们收到错误的原因Call to undefined method Symfony\Component\HttpFoundation\Response::header()

下面是我用来解决此错误的代码,现在一切正常。我希望它可以帮助其他开发人员了解如何修复它然后适应他们的代码。

$response = $next($request);
$IlluminateResponse = 'Illuminate\Http\Response';
$SymfonyResopnse = 'Symfony\Component\HttpFoundation\Response';
$headers = [
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
    'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers',
];

if($response instanceof $IlluminateResponse) {
    foreach ($headers as $key => $value) {
        $response->header($key, $value);
    }
    return $response;
}

if($response instanceof $SymfonyResopnse) {
    foreach ($headers as $key => $value) {
        $response->headers->set($key, $value);
    }
    return $response;
}

return $response;

在我的Kernel.php

protected $middleware = [
    \App\Http\Middleware\Cors::class,
    // ....
];

答案 1 :(得分:7)

似乎从您的应用程序中获得HttpFoundation\Response,它没有header方法。因此,您可以尝试将标头设置为headers的{​​{1}}变量。

HttpFoundation\Response

答案 2 :(得分:0)

我这样做时遇到了这个问题

return Response::stream($callback, 200, $headers);

我可以通过在Response这样的前面加上斜杠(\)来解决此问题:

return \Response::stream($callback, 200, $headers);

尝试以下解决方案:谢谢