Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davro12345's avatar

How to configure CORS to allow requests from frontend running locally

Hi everyone.

I am new to Laravel, so please have patience with me if this seems like a stupid question. I am currently trying to build an API in laravel. At the moment I can't figure out how to configure cors so that the API will accept request from my frontend that is currently running locally.

Lots of material I have read online refer to modifying files and folders that are not included in my app. Maybe I have done something wrong when creating the app, but I have tried recreating it multiple times

0 likes
2 replies
vincent15000's avatar

Is the frontend completely distinct from the backend ?

I mean ... are the back and the front on distinct domains ?

SaaSMediaForce's avatar

Configure CORS in config/cors.php In recent Laravel versions (Laravel 7 and above), CORS support is included by default. Open the config/cors.php file and set up CORS to allow requests from your local frontend.

return [
    'paths' => ['api/*'], // Allows CORS on API routes
    'allowed_methods' => ['*'], // Allow all HTTP methods (GET, POST, etc.)
    'allowed_origins' => ['YOUR_FRONTEND_URL:3000'], // Replace with your frontend's URL
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'], // Allow all headers
    'exposed_headers' => [],
    'max_age' => 0,
    'supports_credentials' => true, // Set to true if your API requires cookies or authorization headers
];

Clear the configuration cache After modifying the configuration, clear Laravel’s configuration cache to make sure changes are applied:

php artisan config:cache

Test your setup Now try making requests from your frontend to your API. This configuration should allow requests from your local frontend.

If you continue to encounter issues, ensure your API routes are under the api prefix (routes/api.php) to match the configuration, or adjust the paths setting to match your specific routes.

1 like

Please or to participate in this conversation.