How to correctly configure CORS in Laravel 12.13 with the HandleCors middleware in bootstrap/app.php?

How to correctly configure CORS in Laravel 12.13 with the HandleCors middleware in bootstrap/app.php?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Bootstrap/app.php vs config/cors.php Conflict in Laravel 12

Laravel 12 introduced a significant shift in how you handle global middleware, moving from the traditional Kernel.php to the functional builder in bootstrap/app.php. While this modernizes the application bootstrap process, it creates a specific architectural trap when configuring Cross-Origin Resource Sharing (CORS). The error you are seeing—specifically, the absence of the Access-Control-Allow-Origin header on preflight requests—is almost certainly caused by a fundamental misunderstanding of how that new middleware registration interacts with the configuration file.

In your current setup, you have appended \Illuminate\Http\Middleware\HandleCors::class to the global middleware stack in bootstrap/app.php. However, you have then attempted to define specific CORS rules in config/cors.php. This creates a contradiction: the native HandleCors middleware expects its configuration to be loaded from config/cors.php, but in Laravel 12's default bootstrap flow, this file is not automatically loaded unless explicitly requested.

The critical issue lies in how the framework loads configuration files. In previous versions, you would register the HandleCors middleware and then load the config file inside a route closure or controller using config('cors'). In Laravel 12, the architecture expects the configuration to be available globally if the middleware is part of the stack. However, the default bootstrap process does not automatically include config/cors.php unless you explicitly tell it to.

To resolve this, you must ensure the configuration file is loaded before the middleware runs. You can achieve this by adding a specific line in your bootstrap/app.php within the withRouting chain or by ensuring the config package is active. More importantly, verify that the HandleCors middleware is actually finding the configuration array at runtime. If the file config/cors.php exists but isn't loaded into the application's config bag, the middleware will fall back to its strict default behavior (allowing no cross-origin requests), resulting in the exact error you are seeing.

You also need to scrutinize the logic within your config/cors.php file regarding the paths configuration. You have set 'paths' => ['api/v1/*']. This means the middleware will only apply these CORS rules if the request URI matches that pattern. If your frontend is hitting /api/v1/data, it works. If you are testing against a root route like /api/v1/test but the middleware logic filters out non-matching paths, no headers are sent. Ensure the URL you are testing in your browser matches one of the paths defined in this array exactly.

Another common pitfall involves the allowed_origins_patterns. You have left this as an empty array while defining specific origins. This is fine for exact matches, but ensure that the origin you are sending requests from (e.g., https://annaponsprojects.com) matches the string in allowed_origins exactly, including the protocol and trailing slash nuances.

If you continue to see the preflight failure after ensuring config/cors.php is loaded, consider adding a custom middleware that explicitly handles CORS headers if you need more granular control than what the native package offers immediately out of the box. However, sticking to the native HandleCors is preferred for security and maintainability in Laravel 12.

For teams looking to manage complex API architectures with multiple environments or microservices, maintaining strict separation between global middleware definitions and environment-specific CORS rules becomes vital. You might want to explore how LaravelCompany structures their boilerplates to handle these modern bootstrap patterns while retaining flexibility for different deployment targets. Their resources often highlight best practices for integrating bootstrap/app.php changes without breaking existing API contracts.

Finally, always clear your configuration cache after making changes to config/cors.php. The error you encountered might persist in the opcode cache or config cache even if you updated the file. Running php artisan config:clear and php artisan cache:clear is mandatory before testing, but remember that this step must be repeated every time you modify the configuration array or move the middleware files.

Our network