title: Error CORS Laravel -- Access-Control-Allow-Origin
body: <p>I have developed a rest Laravel API with Angular.</p>
  
  <ul>
  <li>Laravel: -> 5.7 * </li>
  <li>Angular: -> 8.2.9</li>
  <li>__________Angular CLI: 8.3.8</li>
  </ul>
  
  <p>The project is separated by 2 folders each with its different technology.</p>
  
  <p>Once the project is finished, move it to production with the IONOS server.</p>
  
  <p>But I got this error:</p>
  
  <pre><code>Access to XMLHttpRequest at 'https://api.alfarim.es/category' from origin 'http://www.alfarim.es' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
  app.component.ts:63 
  
  HttpErrorResponse
  
  api.alfarim.es/category:1 Failed to load resource: net::ERR_FAILED
  </code></pre>
  
  <h2>1st - Create a Subdomain</h2>
  
  <ol>
  <li>I uploaded the Laravel project to a folder </li>
  <li>Create a Subdomain in IONOS</li>
  <li>Create the subdomain that points to the Laravel public folder</li>
  <li>Modify Angular's global variable to match Subdomain.</li>
  </ol>
  
  <h2>2nd - Middleware</h2>
  
  <p>// We create a middleware</p>
  
  <pre><code>-&gt;header('Access-Control-Allow-Origin', '*’)
  -&gt;header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS’);
  </code></pre>
  
  <p>// we enter the Kernel</p>
  
  <pre><code>'cors' =&gt; \App\Http\Middleware\Cors::class,
  </code></pre>
  
  <p>// Add to routes
   </p>
  
  <pre><code>Route::group(['middleware' =&gt; 'cors'], function(){
      //rutes
  });
  </code></pre>
  
  <p>And then doing all the settings</p>
  
  <p>Link: <a href="https://github.com/fruitcake/laravel-cors" rel="nofollow noreferrer">https://github.com/fruitcake/laravel-cors</a></p>
  
  <h2>3rd - Fruitcake</h2>
  
  <p>Installing</p>
  
  <pre><code>composer require fruitcake / laravel-cors
  </code></pre>
  
  <p>And then doing all the settings</p>
  
  <p>Link: <a href="https://github.com/fruitcake/laravel-cors" rel="nofollow noreferrer">https://github.com/fruitcake/laravel-cors</a></p>
  
  <hr>
  
  <h2>Actual state</h2>
  
  <p><strong>Middleware</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Middleware;
  
  use Closure;
  
  class Cors
  {
      /**
       * Handle an incoming request.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  \Closure  $next
       * @return mixed
       */
      public function handle($request, Closure $next)
      {
          return $next($request);
          -&gt;header('Access-Control-Allow-Origin', '*’)
          -&gt;header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS’);
  
      }
  }
  </code></pre>
  
  <p><strong>Kernel</strong></p>
  
  <pre><code>protected $middleware = [
        ....
        \Fruitcake\Cors\HandleCors::class, 
      ];
  
  protected $routeMiddleware = [
          .....
          'cors' =&gt; \App\Http\Middleware\Cors::class,
      ];
  </code></pre>
  
  <p><strong>cors.php --- config</strong></p>
  
  <pre><code>    'paths' =&gt; ['api/*'],
  
      /*
      * Matches the request method. `[*]` allows all methods.
      */
      'allowed_methods' =&gt; ['*'],
  
      /*
       * Matches the request origin. `[*]` allows all origins.
       */
      'allowed_origins' =&gt; ['*'],
  
      /*
       * Matches the request origin with, similar to `Request::is()`
       */
      'allowed_origins_patterns' =&gt; [],
  
      /*
       * Sets the Access-Control-Allow-Headers response header. `[*]` allows all headers.
       */
      'allowed_headers' =&gt; ['*'],
  
      /*
       * Sets the Access-Control-Expose-Headers response header with these headers.
       */
      'exposed_headers' =&gt; [],
  
      /*
       * Sets the Access-Control-Max-Age response header when &gt; 0.
       */
      'max_age' =&gt; 0,
  
      /*
       * Sets the Access-Control-Allow-Credentials header.
       */
      'supports_credentials' =&gt; false,
  
      $app-&gt;configure('cors');
  ];
  </code></pre>
  
  <p><strong>Routes</strong></p>
  
  <pre><code>Route::group(['middleware' =&gt; 'cors'], function(){
      //.....
      Route::post('/api/register/','UserController@register');
      }); 
  </code></pre>