dbiljak's avatar

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?

0 likes
7 replies
topvillas's avatar

Did you update to v7 or create a new v7 project. You should have a cors config file if you created a new project.

topvillas's avatar

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.

dbiljak's avatar

\Fruitcake\Cors\HandleCors::class,

"message": "Target class [Fruitcake\Cors\HandleCors] does not exist.",

I know what I have to do, but ...

topvillas's avatar
Level 46

Are you sure it was a v7 app you created?

Spiral's avatar
/*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,        
];
dbiljak's avatar

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.