EnableCrossRequestMiddleware.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. $IlluminateResponse = 'Illuminate\Http\Response';
  17. $SymfonyResopnse = 'Symfony\Component\HttpFoundation\Response';
  18. $headers = [
  19. 'Access-Control-Allow-Origin' => '*',
  20. 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, PATCH, DELETE',
  21. 'Access-Control-Allow-Headers' => 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers, X-CSRF-TOKEN'
  22. ];
  23. if ($response instanceof $IlluminateResponse) {
  24. foreach ($headers as $key => $value) {
  25. $response->header($key, $value);
  26. }
  27. return $response;
  28. }
  29. if ($response instanceof $SymfonyResopnse) {
  30. foreach ($headers as $key => $value) {
  31. $response->headers->set($key, $value);
  32. }
  33. return $response;
  34. }
  35. return $response;
  36. // $response = $next($request);
  37. //
  38. // $response->header('Access-Control-Allow-Origin', '*');
  39. // $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
  40. // $response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
  41. // $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
  42. // $response->header('Access-Control-Allow-Credentials', 'true');
  43. //
  44. // return $response;
  45. //
  46. // $response = $next($request);
  47. // if ($response instanceof Response) {
  48. // return $next($request)->header('Access-Control-Allow-Origin', '*')
  49. // ->header('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS,PATCH,DELETE,HEAD')
  50. // ->header('Access-Control-Allow-Headers', 'x-csrf-token,x-requested-with,content-type');
  51. // ->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
  52. // ->header('Access-Control-Allow-Credentials', 'true');
  53. // }
  54. // // 如果是实列 \Symfony\Component\HttpFoundation\Response::class
  55. // $response->headers->set('Access-Control-Allow-Origin', '*');
  56. // $response->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS,PATCH,DELETE,HEAD');
  57. // $response->headers->set('Access-Control-Allow-Headers', 'x-csrf-token,x-requested-with,content-type');
  58. // $response->headers->set('Access-Control-Expose-Headers', 'Authorization, authenticated');
  59. // $response->headers->set('Access-Control-Allow-Credentials', 'true');
  60. // return $response;
  61. }
  62. }