EnableCrossRequestMiddleware.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. class EnableCrossRequestMiddleware
  5. {
  6. /**
  7. * Handle an incoming request.
  8. *
  9. * @param \Illuminate\Http\Request $request
  10. * @param \Closure $next
  11. * @return mixed
  12. */
  13. public function handle($request, Closure $next)
  14. {
  15. $response = $next($request);
  16. $response->header('Access-Control-Allow-Origin', '*');
  17. $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
  18. $response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
  19. $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
  20. $response->header('Access-Control-Allow-Credentials', 'true');
  21. return $response;
  22. //
  23. // $response = $next($request);
  24. // if ($response instanceof Response) {
  25. // return $next($request)->header('Access-Control-Allow-Origin', '*')
  26. // ->header('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS,PATCH,DELETE,HEAD')
  27. // ->header('Access-Control-Allow-Headers', 'x-csrf-token,x-requested-with,content-type');
  28. // ->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
  29. // ->header('Access-Control-Allow-Credentials', 'true');
  30. // }
  31. // // 如果是实列 \Symfony\Component\HttpFoundation\Response::class
  32. // $response->headers->set('Access-Control-Allow-Origin', '*');
  33. // $response->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS,PATCH,DELETE,HEAD');
  34. // $response->headers->set('Access-Control-Allow-Headers', 'x-csrf-token,x-requested-with,content-type');
  35. // $response->headers->set('Access-Control-Expose-Headers', 'Authorization, authenticated');
  36. // $response->headers->set('Access-Control-Allow-Credentials', 'true');
  37. // return $response;
  38. }
  39. }