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.
config/app.php
'providers' => [
....
Barryvdh\Cors\ServiceProvider::class,
],
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.
app/Http/Kernel.php
....
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
// ...
\Barryvdh\Cors\HandleCors::class,
],
];
....
I hope you found your best....
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?