spatie/laravel-cors
Adds configurable CORS support to Laravel/Lumen: sets CORS headers on responses, handles preflight requests, and lets you define allowed origins, methods, headers, and credentials via middleware and config. Abandoned since Laravel 7+ has native CORS support.
fruitcake/laravel-cors or Laravel’s native HandleCors).OPTIONS requests natively, critical for APIs with complex payloads (e.g., PUT/DELETE with auth headers).fruitcake/laravel-cors or Laravel’s HandleCors) is actively maintained and preferred for modern stacks.fruitcake/laravel-cors) that offer long-term support?illuminate/http) suffices.composer require spatie/laravel-cors.app/Http/Kernel.php (global or route-specific):
protected $middleware = [
// ...
\Spatie\Cors\Cors::class,
];
config/cors.php to match existing policies (e.g., allowed_methods, allowed_origins).Spatie\Cors\Cors runs after auth middleware (if applicable) but before response formatting (e.g., JSON APIs).fruitcake/laravel-cors).Kernel.php.$app->group(['middleware' => 'cors'], function () {
// Routes here
});
config/cors.php (if it exists) or create a new one.403 Forbidden due to misconfigured origins).dd() or Log::debug() to inspect middleware execution.| Failure Scenario | Impact | Mitigation |
|---|---|---|
Misconfigured allowed_origins |
CORS preflight fails (403) |
Test with * temporarily; validate configs. |
| Middleware registration error | No CORS headers sent | Check Kernel.php; verify Composer autoload. |
| PHP version incompatibility | Package fails to load | Downgrade PHP to 7.2–7.4 or replace package. |
| Laravel upgrade beyond v6 | Package breaks | Migrate to fruitcake/laravel-cors or native CORS. |
config/cors.php structure and middleware ordering.How can I help you explore Laravel packages today?