Laravel 11 CORS problem on localhost with SvelteKit
To answer your question, I'd first like to clarify that as of now, Laravel 11 does not exist, as Laravel 8 is the latest stable release. I assume you meant Laravel 8 or a newer version.
Now, let's address the issue you're encountering with Cross-Origin Resource Sharing (CORS) between Laravel and SvelteKit on localhost.
CORS is a security feature that prevents web applications from making requests to a different domain unless the server explicitly allows it. In your case, you're trying to make requests from your SvelteKit application running on localhost to your Laravel backend also running on localhost. Since they are on the same origin, you might think CORS shouldn't be an issue. However, Laravel's default CORS configuration only allows requests from specific domains, not localhost.
To resolve this issue, follow these steps:
-
Install Laravel's CORS package: If you haven't already, install the Laravel CORS package using Composer:
composer require laravel/sanctum composer require laravel/cors
Make sure to add the following lines to your
composer.json
file:"autoload": { "psr-4": { "App\\": "app/", "Laravel\\": "laravel/" } }, "require": { "laravel/cors": "^3.0" }
Run
composer install
to install the packages. -
Configure Laravel's CORS: Open your
config/app.php
file and add the following line to theproviders
array:'providers' => [ // ... Laravel\Cors\ServiceProvider::class, ],
Next, open your
config/cors.php
file and update the$allowedOrigins
array to include localhost:'allowedOrigins' => [ '*', // Allow all origins for local development ],
Be aware that allowing all origins can pose a security risk. For production, you should restrict the allowed origins to trusted domains.
-
Update Laravel's middleware: Open your
app/Http/Kernel.php
file and add the following line to the$routeMiddleware
array:'cors' => \Laravel\Cors\HandleCors::class,
Next, add the following line to the
$middlewareGroups
array:'web' => [ // ... \Laravel\Cors\HandleCors::class, ],
-
Configure SvelteKit: In your SvelteKit project, open the
svelte.config.js
file and add the following line to thekit.config.js
object:cors: { url: 'http://localhost:8000', // Replace with your Laravel backend URL allowCredentials: true, },
-
Test the setup: Now, you should be able to make requests from your SvelteKit application to your Laravel backend without encountering CORS issues. To test it, you can create an API route in Laravel and make a request from SvelteKit to that route.
For example, in Laravel, create a new route in
routes/api.php
:Route::get('/test', function () { return response()->json(['message' => 'Hello from Laravel!']); });
In your SvelteKit application, create a new file
src/api/test.js
and add the following code:import fetch from 'isomorphic-unfetch'; export async function getServerSideProps() { const res = await fetch('http://localhost:8000/api/test'); const data = await res.json(); return { props: { data } }; } export default function TestPage({ data }) { return <div>{JSON.stringify(data)}</div>; }
This code makes a request to the Laravel route you created and returns the response as a prop to the
TestPage
component.Finally, create a new page
src/pages/test.svelte
and import theTestPage
component:<script lang="ts"> import TestPage from '$lib/api/test'; export let { data }; </script> <TestPage {data} />
Run your SvelteKit application and visit the
/test
page in your browser. You should see the response from Laravel displayed on the page.
That's it! You've successfully set up CORS between your Laravel backend and SvelteKit application on localhost. Remember to remove the *
from the $allowedOrigins
array in your config/cors.php
file when you're ready to deploy your Laravel application to a production environment.