Laravel 5.1 API Enable Cors
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Comprehensive Guide on Enabling CORS for Laravel 5.1 API
Introduction:
In today's world of dynamic web applications and cross-origin concerns, enabling Cross-Origin Resource Sharing (CORS) has become an essential part of modern web development. When working with APIs, you need to address this issue to ensure that your users can access the data from different origins without any hassle. In this blog post, we will walk through a step-by-step process on how to enable CORS for Laravel 5.1 API using different approaches and libraries available.
Approach 1: Using cors-illuminate (https://github.com/neomerx/cors-illuminate)
Caution: The code snippets given below assume you have a basic understanding of Laravel 5.1 routing and configuration files. If not, feel free to check out the getting started guide on the official Laravel documentation (https://laravel.com).
Step 1: Install the cors-illuminate library using Composer:
```bash
composer require neomerx/cors-illuminate
```
Step 2: Add the cors-illuminate service provider to your "app/config/app.php" file under providers array section:
```php
'providers' => [
// ...
Neomerx\Cors\Facades\Cors::class,
],
```
Step 3: Add the cors-illuminate facade to your "app/config/app.php" file under aliases section:
```php
'aliases' => [
// ...
'Cors' => Neomerx\Cors\Facades\Cors::class,
],
```
Step 4: In your route files (either web.php or api.php), create a middleware group named "api" to handle CORS:
```php
['cors'], // use cors-illuminate middleware in your routes
], function () {
// your API routes...
});
```
Step 5: In the same route file, add the CORS middleware group to your specific controller or API calls using the "Cors" facade:
```php
json([
'name' => 'John Doe',
'age' => 30,
]);
})->middleware(Cors::class)->where('cors_origin', '*')->options(function ($request) {
return (new \Illuminate\Http\Response())->setStatusCode(204);
});
```
Approach 2: Using Laravel-CORS (https://github.com/barryvdh/laravel-cors)
Step 1: Install the laravel-cors library using Composer:
```bash
composer require barryvdh/laravel-cors
```
Step 2: Add the Laravel-CORS Service Provider to your "app/config/app.php" file under providers array section:
```php
'providers' => [
// ...
Barryvdh\LaravelIdea\IdeHelperServiceProvider::class,
],
```
Note that the "Barryvdh\LaravelIdea\IdeHelperServiceProvider" is required as it provides helper functions for Laravel Idea integration. However, we will not discuss further on this topic.
Step 3: Add the Laravel-CORS middleware to your "app/config/app.php" file under aliases section:
```php
'aliases' => [
// ...
'LaravelCors' => Barryvdh\LaravelIdea\Console\Cors::class,
],
```
Step 4: In your route files (either web.php or api.php), create a middleware group named "api" to handle CORS:
```php
['cors'], // use laravel-cors middleware in your routes
], function () {
// your API routes...
});
```
Step 5: In the same route file, add the CORS middleware group to your specific controller or API calls using the "Cors" facade (same as above):
```php