调用未定义的方法Symfony \ Component \ HttpFoundation \ Response :: header() [英] Call to undefined method Symfony\Component\HttpFoundation\Response::header()
问题描述
我使用的是cors中间件,在我添加Laravel Passport之前,它似乎可以正常工作,现在出现了问题..它显示了错误
Hi i was using a cors middleware which seems to work fine until i added Laravel Passport now there is a problem with it.. it shows the error
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条件之后..谢谢您的任何帮助
the issue is after the if condition .. Any help will be appreaciated thanks
推荐答案
我遇到了同样的问题.似乎这是Passport中的错误,并且许多开发人员处于相同的情况.我刚刚找到了解决此问题的方法.出现此错误的原因是,我们从 middleware
中获得的Response对象通常是 Illuminate \ Http \ Response
类的实例,可以使用 header('Header-Key','Header-Value')
,而Passport处理的请求将是 Symfony \ Component \ HttpFoundation \ Response
的实例,这就是为什么错误调用未定义的方法Symfony \ Component \ HttpFoundation \ Response :: header()
Hi I faced the same problem. It seems like it was an error in Passport and there are many developers are in the same situation. I just found the solution to this issue. The reason why we get this error is because Response object we get in middleware
is usually an instance of Illuminate\Http\Response
class which we can set Response headers using the method header('Header-Key', 'Header-Value')
whereas the Request handled by Passport will be an instance of Symfony\Component\HttpFoundation\Response
that's why we got the error Call to undefined method Symfony\Component\HttpFoundation\Response::header()
下面是我用来解决此错误的代码,现在一切正常.我希望它可以帮助其他开发人员了解如何修复它,然后适应他们的代码.
Below is the code that I use to tackle this error and now everything works fine. I hope it helps other developers get the idea how to fix it and then adapt to their code.
$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,
// ....
];
这篇关于调用未定义的方法Symfony \ Component \ HttpFoundation \ Response :: header()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!