Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable CORS in Laravel?

I am in Laravel 5.8 - I kept getting this CORS issue

I've tried

php artisan make:middleware Cors

Add these code

<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
  public function handle($request, Closure $next)
  {
    return $next($request)
      ->header(‘Access-Control-Allow-Origin’, ‘*’)
      ->header(‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, DELETE, OPTIONS’)
      ->header(‘Access-Control-Allow-Headers’, ‘X-Requested-With, Content-Type, X-Token-Auth, Authorization’);
  }
}

restart my local Apache 2 sudo apachectl -k restart

Open up app/Http/Kernel.php - added this 1 line

protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'admin' => \App\Http\Middleware\AdminMiddleware::class,
        'dev' => \App\Http\Middleware\DevMiddleware::class,
        'cors' => \App\Http\Middleware\Cors::class, <----- 
    ];

refresh the site, go to console, still see the same CORS issue

How would one go about and debug this further?

like image 617
code-8 Avatar asked Mar 25 '19 20:03

code-8


People also ask

How do I add CORS to my Laravel project?

To do so, open a terminal or command prompt, navigate to your project directory, and run the following command: Then, make sure that the CORS class is part of your global middleware stack. Again, adding the middleware is only necessary for the older versions of Laravel in which you installed the library yourself.

What is Cross-Origin Resource Sharing (CORS) in Laravel?

Read more: Laravel JWT Token-Based Authentication with Angular Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. How to enable CORS in your REST API backend? First, we have to install a fresh Laravel app.

What is Cors authentication in Laravel?

CORS authenticate the coherence between two different domains. Read more: Laravel JWT Token-Based Authentication with Angular Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.

Do I need to enable Cors If I am using other versions?

If you are using other versions of Laravel, make sure to read below. Laravel 7 has been released on March and provides built-in support for CORS so developers don't need to use third party packages to enable CORS in their laravel apps.


3 Answers

First solution

Try to set the CORS middleware as a global middleware.

the handle function in the CORS middleware:

 public function handle($request, Closure $next)
 {
  return $next($request)
   ->header('Access-Control-Allow-Origin', '*')
   ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
   ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
 }

to add this middleware globally, go to App\Http\Kernel, and add this line in the $middleware array:

\App\Http\Middleware\Cors::class,

Second solution

you can also add this code in the bootstrap/app.php

header('Access-Control-Allow-Origin', '*');
header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS');
header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

hope it works!

like image 174
Behzad Pournouri Avatar answered Oct 22 '22 09:10

Behzad Pournouri


Try laravel-cors package that allows you to send Cross-Origin Resource Sharing headers with Laravel middleware configuration.

like image 10
ArtemSky Avatar answered Oct 22 '22 10:10

ArtemSky


Add below to you .htaccess (just add to the destination site and origin site)

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

Hope it saves someone time, happy coding!!!

like image 2
The Billionaire Guy Avatar answered Oct 22 '22 10:10

The Billionaire Guy