CORS on Laravel 7
I can't find any documentation for the CORS setup on Laravel 7.
I don't have cors.php in the config folder, I added this to my routes/api.php:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization');
Is there a more elegant solution?
Did you update to v7 or create a new v7 project. You should have a cors config file if you created a new project.
A new project, and no cors file
That's odd! Try going to the Laravel git repo and grabbing the config file from there.
Make sure you have the middleware and that it's enabled too.
\Fruitcake\Cors\HandleCors::class,
"message": "Target class [Fruitcake\Cors\HandleCors] does not exist.",
I know what I have to do, but ...
Are you sure it was a v7 app you created?
/*installation Command*/
composer require barryvdh/laravel-cors => /*v0.11.4*/
/*config/app.php*/ $providers
Barryvdh\Cors\ServiceProvider::class,
/*Publish barryvdh/laravel-cors package file*/
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
/*Use CORS Middleware*/ => /*app/Http/Kernel.php*/
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
'throttle:60,1',
'bindings',
\Barryvdh\Cors\HandleCors::class,
],
];
protected $routeMiddleware = [
'cors' => \Barryvdh\Cors\HandleCors::class,
];
You were right, it didn't even cross my mind. After upgrade it is all OK now.
thnx
Please or to participate in this conversation.