Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I configure routes in Laravel to use Svelte for all routes except '/admin'


How do I configure routes in Laravel to use Svelte for all routes except '/admin'


To configure your Laravel routes to use Svelte for all routes except '/admin', you can follow these steps:

1. Set up Inertia.js with Svelte:
- Install Inertia.js and the Svelte adapter:

bash
     composer require inertiajs/inertia-laravel
     npm install --save-dev @inertiajs/inertia @inertiajs/inertia-svelte
     

- Configure Inertia.js in your `app.js` file:
javascript
     import { createInertiaApp } from '@inertiajs/inertia-svelte';

     createInertiaApp({
       resolve: (name) => require(`./Pages/${name}`),
       setup({ el, App, props }) {
         new App({ target: el, props });
       },
     });
     

2. Set up Jetstream with Vue.js for the '/admin' route:
- Install Jetstream and configure it to use Vue.js:

bash
     composer require laravel/jetstream
     php artisan jetstream:install inertia --teams
     

- Update your `app.js` file to handle both Svelte and Vue.js:
javascript
     import { createInertiaApp } from '@inertiajs/inertia-svelte';
     import { createInertiaApp as createInertiaAppVue } from '@inertiajs/inertia-vue3';

     createInertiaApp({
       resolve: (name) => require(`./Pages/${name}`),
       setup({ el, App, props }) {
         new App({ target: el, props });
       },
     });

     createInertiaAppVue({
       resolve: (name) => require(`./Pages/Admin/${name}`),
       setup({ el, App, props }) {
         new App({ render: (h) => h(App, props) }).mount(el);
       },
     });
     

3. Configure your routes:
- In your `routes/web.php` file, set up the routes:

php
     use Illuminate\Support\Facades\Route;
     use Inertia\Inertia;

     Route::get('/', function () {
       return Inertia::render('Home');
     })->name('home');

     Route::middleware(['auth:sanctum', 'verified'])->prefix('admin')->group(function () {
       Route::get('/', function () {
         return Inertia::render('Admin/Dashboard');
       })->name('admin.dashboard');
     });
     

In this setup, all routes except the '/admin' route will use Svelte as the frontend framework, while the '/admin' route will use Vue.js (provided by Jetstream).

The key points are:

1. Set up Inertia.js with Svelte as the default frontend framework.
2. Set up Jetstream with Vue.js for the '/admin' route.
3. Configure the routes to use the appropriate frontend framework based on the URL.

This way, you can have a consistent user experience across your application, while still allowing for a separate admin area powered by Vue.js.

Citations:
[1] https://github.com/tighten/ziggy/issues/398
[2] https://github.com/wewowweb/laravel-svelte-preset/issues/5
[3] https://clouddevs.com/svelte/routing/
[4] https://stackoverflow.com/questions/76581070/how-to-configure-laravel-with-jetstream-inertia-svelte-front-end-and-vue-ad
[5] https://rappasoft.com/blog/creating-a-laravel-10-application-using-inertia-js-svelte-and-tailwind-css