Answers for "Easiest Way to Allow CORS in Laravel 6 or Any Version"

PHP
0

Easiest Way to Allow CORS in Laravel 6 or Any Version

Step 1: php artisan make:middleware Cors

Step 2: Now open Cors.php from App\Http\Middleware folder and replace handle() function with this code:

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, Authorizations');
}

Step 3: Lastly, open Kernel.php from App\Http folder add the below line to the $middleware array:

protected $middleware = [
    ...
    \App\Http\Middleware\Cors::class,
];

Now run the application and call API from anywhere.
Posted by: Guest on September-22-2021

Code answers related to "Easiest Way to Allow CORS in Laravel 6 or Any Version"

Browse Popular Code Answers by Language