DEV Community

Pradeep Kumar
Pradeep Kumar

Posted on • Updated on

Solved: Laravel Cors Issue

Laravel 10

If config/cors.php not working, then simply add following to your public/index.php:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header("Access-Control-Allow-Headers: *");
Enter fullscreen mode Exit fullscreen mode

and update config/cors.php to

'allowed_origins' => [],
Enter fullscreen mode Exit fullscreen mode

Laravel 9 and lower

Step 1. Add \Fruitcake\Cors\HandleCors::class, in app/Http/Middleware/Kernel.php in $middleware array

eg:

protected $middleware = [
        ...
        \Fruitcake\Cors\HandleCors::class,
        ...
    ];
Enter fullscreen mode Exit fullscreen mode

Step 2. Add cors.php file in config folder and write following code:

<?php

return [
    'paths' => ['api/*', 'sanctum/csrf-cookie'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];

Enter fullscreen mode Exit fullscreen mode

Top comments (0)