easiest way to allow cors in laravel 6 or any version

Solutions on MaxInterview for easiest way to allow cors in laravel 6 or any version by the best coders in the world

showing results for - "easiest way to allow cors in laravel 6 or any version"
Chloé
09 May 2016
1Step 1: php artisan make:middleware Cors
2
3Step 2: Now open Cors.php from App\Http\Middleware folder and replace handle() function with this code:
4
5public function handle($request, Closure $next)
6{
7    return $next($request)
8        ->header('Access-Control-Allow-Origin', '*')
9        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
10        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorizations');
11}
12
13Step 3: Lastly, open Kernel.php from App\Http folder add the below line to the $middleware array:
14
15protected $middleware = [
16    ...
17    \App\Http\Middleware\Cors::class,
18];
19
20Now run the application and call API from anywhere.