在安装完laravel跨域组件后,报错:
prod.ERROR: Call to undefined method Symfony\Component\HttpFoundation\Response::header() {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to undefined method Symfony\\Component\\HttpFoundation\\Response::header() at /data/www/apis.huatu.com/app/Http/Middleware/EnableCrossRequestMiddleware.php:21) [stacktrace]
解决办法如下:
1、打开/app/Http/Middleware/EnableCrossRequestMiddleware.php
2、找到handle方法
3、将handle内容替换如下
$response= $next($request); $IlluminateResponse= 'Illuminate\Http\Response'; $SymfonyResopnse= 'Symfony\Component\HttpFoundation\Response'; $origin= $request->server('HTTP_ORIGIN')? $request->server('HTTP_ORIGIN'): '*'; $headers= [ 'Access-Control-Allow-Origin' => $origin, 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, OPTIONS, DELETE', 'Access-Control-Allow-Headers' => 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN, User-Agent, Host, Accept-Encoding, Connection, Content-Length', 'Access-Control-Expose-Headers'=>'Authorization', 'Access-Control-Allow-Credentials'=>'true', 'Access-Control-Max-Age'=>'3600' ]; 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; }
文件参考:https://www.jianshu.com/p/22a82f8dd414