Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Laravel CORS settings have no effect: missing config/cors.php, paths and cache

Errorhas been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource

Short answer

Laravel answers CORS preflights itself through the HandleCors middleware, but only for URLs listed in the paths option of config/cors.php. That file is not published by default. Publish it, add your route pattern, set supports_credentials for cookie logins, and clear the config cache.

  • Applies to Laravel 11 and 12, PHP 8.3, Laravel Sanctum 4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send the preflight to the exact URL the frontend calls and look at the response headers. No Access-Control headers means Laravel is not applying CORS to that path.

    curl -s -o /dev/null -D - -X OPTIONS http://localhost:8000/api/user \
      -H "Origin: http://localhost:5173" \
      -H "Access-Control-Request-Method: GET"
  2. 2Match what you see, then jump to its fix

    1. 1The preflight for /api/... works but a route outside api/* returns no CORS headers.The route path is not covered by the paths optionFix
    2. 2You edited a CORS setting, but the config:show output and the headers are unchanged.config/cors.php was never published, or the config is cachedFix
    3. 3Firefox reports: Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*', or the login call is blocked.Cookie login from an SPA while supports_credentials is falseFix
    4. 4The request returns 404 and routes/api.php does not exist in a new Laravel 11 or later app.The project has no API routes yetFix

What does the error look like?

  • has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
  • Response to preflight request doesn't pass access control check
  • Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*'

Cause 1 of 4

The route path is not covered by the paths option

1Check

The default cors configuration lists paths as api/* and sanctum/csrf-cookie only. A route such as /login or /graphql gets no CORS headers.

php artisan config:show cors

2Fix

Publish the file, then add your path pattern to paths.

php artisan config:publish cors

// config/cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie', 'login', 'logout'],

3Verify

The preflight now returns Access-Control-Allow-Origin.

curl -s -o /dev/null -D - -X OPTIONS http://localhost:8000/login \
  -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: POST" | grep -i "^access-control"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

config/cors.php was never published, or the config is cached

1Check

In current Laravel, cors.php is not published by default. Also, when configuration is cached, edits and .env changes are not read until the cache is rebuilt.

ls config/cors.php; php artisan config:show cors

2Fix

Publish the file if it is missing, then clear the configuration cache, and rebuild it on production after your change.

php artisan config:publish cors
php artisan config:clear
# production, after deploying
php artisan config:cache

3Verify

php artisan config:show cors prints your edited values.

php artisan config:show cors

Confirmed by laravel.com

Did this fix it?

Cause 3 of 4

Cookie login from an SPA while supports_credentials is false

1Check

Sanctum's SPA login needs the response header Access-Control-Allow-Credentials: True. The default supports_credentials value is false, and a wildcard origin cannot be used with credentials.

grep -n "supports_credentials\|allowed_origins" config/cors.php

2Fix

Set supports_credentials to true, list the SPA origin explicitly, and enable withCredentials and withXSRFToken on the HTTP client.

// config/cors.php
'allowed_origins' => ['http://localhost:5173'],
'supports_credentials' => true,

// resources/js/bootstrap.js (axios)
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;

3Verify

The preflight shows Access-Control-Allow-Credentials: true and the exact origin.

curl -s -o /dev/null -D - -X OPTIONS http://localhost:8000/sanctum/csrf-cookie \
  -H "Origin: http://localhost:5173" -H "Access-Control-Request-Method: GET" | grep -i "^access-control"

Confirmed by laravel.com

Did this fix it?

Cause 4 of 4

The project has no API routes yet

1Check

New Laravel applications since version 11 do not ship API routing by default. Without it the api/* paths do not exist, so there is nothing for CORS to apply to.

ls routes; php artisan route:list --path=api

2Fix

Install API routing, which also installs Sanctum.

php artisan install:api

3Verify

routes/api.php exists and route:list shows your api routes.

php artisan route:list --path=api

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Commit config/cors.php so the allowed origins are reviewed like code.
  • Run config:clear locally whenever a setting seems to be ignored.
  • Never combine a wildcard origin with credentials.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Back end

Browser blocks your Laravel API with a CORS error

Response to preflight request doesn't pass access control check

Laravel answers CORS preflights itself through its HandleCors middleware, driven by config/cors.php.

3 causes, each with a fix

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.