Laravel cors middleware using barryvdh/laravel-cors example

March 15, 2018 | Category : Laravel 5.6 Laravel 5.5 Laravel 5 Laravel PHP

If you want to add cors middleware in your laravel application then you are a right place. here i will quick guide to add cors middleware using barryvdh/laravel-cors composer package in laravel 5.6 application. cors is important to prevent other domain browsing request.

In this example we will use barryvdh/laravel-cors composer package and you will have cors middleware for your every api route of your application. So let's simple proceed with bellow step for installation and how to add with middleware.

Install barryvdh/laravel-cors Package:

so basically we will install barryvdh/laravel-cors package by following composer command in your laravel 5 application.

composer require barryvdh/laravel-cors

After successfully install package, open config/app.php file and add service provider and alias.


'providers' => [




Package Configuration:

now we require to configuration of barryvdh/laravel-cors package file. So let's run bellow command for make configuration file.

php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"

now you have cors.php file in config folder.

Use CORS Middleware:

now we will use cors middleware facade in Kernel.php file. so let's add following line on Kernel.php file.



protected $middlewareGroups = [

'web' => [

// ...


'api' => [

// ...





I hope you found your best....