Laravel Cors

screenshot of Laravel Cors

Send CORS headers in a Laravel application

Overview:

The Spatie Laravel CORS package is designed to add Cross-Origin Resource Sharing (CORS) headers to responses in your Laravel or Lumen application. The package offers support for preflight requests and allows for easy configuration to suit your specific needs.

Features:

  • CORS Headers: Automatically adds CORS headers to Laravel or Lumen app responses.
  • Preflight Requests: Supports preflight requests for enhanced compatibility.
  • Customizable Configuration: Easily configurable to specify allowed origins and customize CORS profiles.

Laravel:

Install the package via Composer:

composer require spatie/laravel-cors

The package will auto-register its service provider. Register the provided Spatie\Cors\Cors middleware in the global middleware group. The default configuration file will be available at config/cors.php.

Lumen:

Install the package via Composer:

composer require spatie/laravel-cors

Copy the config file from the vendor directory. Register the config file, middleware, and service provider in bootstrap/app.php.

Summary:

The Spatie Laravel CORS package simplifies the process of adding CORS headers to responses in Laravel or Lumen applications. With support for preflight requests and customizable configuration options, it provides a flexible solution for managing cross-origin resource sharing. By following the installation guide and utilizing the middleware, users can enhance their API routes with proper CORS headers to improve interaction with client applications.