title: How to add CORS headers for OPTIONS preflight request in Laravel Lumen 5.5.2 with angular4 as front end
body: <p>I am running laravel lumen php frameowrk 5.5.2 on localhost\8080 on my machine.I am running angular4 frontend locally on localhost\4200. When I run my application, I am able to connect to some Laravel apis through angular and get the data and also I can make changes to those apis. But when i try to connect to some other apis in Laravel through my front-end, I am getting this error in the browser console  "Access to XMLHttpRequest at '<a href="http://localhost:8080/ABC" rel="nofollow noreferrer">http://localhost:8080/ABC</a>' from origin '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."</p>
  
  <p>So i referred to this link and made respective changes at the Laravel backend server code as mentioned by them:
  <a href="https://gist.github.com/danharper/06d2386f0b826b669552#file-usage-md" rel="nofollow noreferrer">https://gist.github.com/danharper/06d2386f0b826b669552#file-usage-md</a></p>
  
  <p>But then, when i connect to Laravel apis, through angular, now I get this error:
  "Access to XMLHttpRequest at '<a href="http://localhost:8080/ABC" rel="nofollow noreferrer">http://localhost:8080/ABC</a>' from origin '<a href="http://localhost:4200" rel="nofollow noreferrer">http://localhost:4200</a>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."</p>
  
  <p>Also, do I have to make any changes to the angular front end code?
  I cannot move forward because of this CORS blocking...Any help would be greatly appreciated.</p>
  
  <p>This is my CorsMiddleware.php file in Laravel:</p>
  
  <p>
  
  <p>use Closure;</p>
  
  <p>class CorsMiddleware
  {</p>
  
  <pre><code>public function handle($request, Closure $next)
  {
  
      $response = $next($request);
      $response-&gt;header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, 
      PATCH, DELETE');
      $response-&gt;header('Access-Control-Allow-Headers', $request- 
      &gt;header('Access-Control-Request-Headers'));
      $response-&gt;header('Access-Control-Allow-Origin', '*');
      return $response;    
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my CatchAllOptionsRequestsProvider.php file in Laravel:</p>
  
  <p>
  
  <p>/**
  * If the incoming request is an OPTIONS request
  * we will register a handler for the requested route
  */</p>
  
  <p>class CatchAllOptionsRequestsProvider extends ServiceProvider {</p>
  
  <pre><code>public function register()
  {
      $request = app('request');
  
      if ($request-&gt;isMethod('OPTIONS'))
      {
         app()-&gt;options($request-&gt;path(), function() { return response('', 
         200); 
      });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my app.php file where i register my provider and Cors middleware:</p>
  
  <p>$app->register(App\Providers\CatchAllOptionsRequestsProvider::class);</p>
  
  <p>$app->routeMiddleware(['auth' => App\Http\Middleware\Authenticate::class,]);</p>
  
  <p>$app->middleware([App\Http\Middleware\CorsMiddleware::class]);</p>