laravel 调用未定义的方法 Symfony\Component\HttpFoundation\Response::header()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43593351/
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
Call to undefined method Symfony\Component\HttpFoundation\Response::header()
提问by Syed Abdur Rehman Kazmi
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
嗨,我正在使用 cors 中间件,它似乎工作正常,直到我添加了 Laravel Passport 现在它有问题......它显示了错误
Call to undefined method Symfony\Component\HttpFoundation\Response::header() on line number 36
This is my middleware :
这是我的中间件:
<?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;
}
}
the issue is after the if condition .. Any help will be appreaciated thanks
问题是在 if 条件之后.. 任何帮助都会得到感谢
回答by Pakpoom Tiwakornkit
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()
嗨,我遇到了同样的问题。似乎是 Passport 中的一个错误,并且有许多开发人员处于相同的情况。我刚刚找到了这个问题的解决方案。我们得到这个错误的原因是因为我们得到的 Response 对象middleware
通常是一个Illuminate\Http\Response
类的实例,我们可以使用该方法设置 Response 标头,header('Header-Key', 'Header-Value')
而 Passport 处理的请求将是一个实例,Symfony\Component\HttpFoundation\Response
这就是我们得到错误的原因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;
And in my Kernel.php
而在我的 Kernel.php
protected $middleware = [
\App\Http\Middleware\Cors::class,
// ....
];
回答by Pankit Gami
It seems that from your application you are getting the HttpFoundation\Response
, which doesn't have the header
method. So instead you can try to set the header to the headers
variable of the HttpFoundation\Response
.
似乎从您的应用程序中您获得了HttpFoundation\Response
没有该header
方法的 。因此,您可以尝试将标头设置headers
为HttpFoundation\Response
.
foreach ($headers as $key => $value)
$response->headers->set($key, $value);
return $response;
回答by Muhammad Wajahat Anwar
I got this problem when I did this
当我这样做时,我遇到了这个问题
return Response::stream($callback, 200, $headers);
I solve it by simply putting forward slash ( \ ) before Response like this:
我通过简单地在 Response 之前放置斜杠( \ )来解决它,如下所示:
return \Response::stream($callback, 200, $headers);
Try this solution: Thanks
试试这个解决方案:谢谢