Skip to content

Use Inertia in a module

At the end, the Blog module renders Inertia pages from Modules/Blog/resources/js/Pages, and the application resolves them by name.

Prerequisites:

  • Inertia v3 is installed in your application as the Inertia documentation describes. The package does not install Inertia, and the stubs target Inertia v3 only.
  • Server: inertiajs/inertia-laravel ^3.
  • Client: @inertiajs/vue3 (Vue 3), @inertiajs/react (React 19), or @inertiajs/svelte (Svelte 5), all ^3.
  • Your root Blade view loads resources/js/app.js with @vite and renders @inertia. With React, place @viteReactRefresh before @vite for npm run dev.
  1. Choose the frontend framework for generated pages. The default is vue.

    To use React or Svelte, publish the configuration and set inertia.frontend. If config/modules.php already exists, edit the existing file.

    Terminal window
    php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=config
    config/modules.php
    'inertia' => [
    'frontend' => 'react', // vue, react or svelte
    ],

    module:make reads this key and accepts no framework flag of its own.

  2. Create the module.

    Terminal window
    php artisan module:make Blog --inertia

    The command generates BlogController and four pages (Index, Create, Show, and Edit) in Modules/Blog/resources/js/Pages. It skips Blade views. Combining --inertia with --plain writes no Inertia files.

  3. Publish the Inertia entry file so Vite resolves pages from every module.

    Terminal window
    php artisan module:publish-inertia --vue

    Pass at most one framework flag, or omit it to use inertia.frontend. The command writes resources/js/app.js and asks before overwriting an existing file. Move any custom code out of resources/js/app.js first, then pass --force to overwrite without a prompt.

    See module:publish-inertia for flag validation, exit codes, and how the published entry mounts the app.

  4. Register your framework plugin in the root vite.config.js. Keep your existing plugins and options, and make sure resources/js/app.js is in laravel({ input: [...] }). Install the plugin package with npm if needed.

    vite.config.js
    import vue from '@vitejs/plugin-vue'
    // in defineConfig({ plugins: [...] }): add the plugin call, keep the rest
    vue(),
    // in your existing laravel({...}) call: input must include the entry file
    // input: ['resources/css/app.css', 'resources/js/app.js'],
  5. Build the assets.

    Terminal window
    npm run build

The generated controller returns Inertia::render('Blog/Index'). The first segment is the module folder name (case-sensitive), and the rest is the path under resources/js/Pages:

Page name File
Blog/Index Modules/Blog/resources/js/Pages/Index.vue
Blog/Posts/Show Modules/Blog/resources/js/Pages/Posts/Show.vue

The file extension matches your framework (.vue, .jsx, or .svelte). If no module page matches, the entry file falls back to resources/js/Pages/<name>.

Keep npm run dev running or run npm run build, then serve the application (for example with php artisan serve). Open /blogs to verify the Index page renders. Run php artisan route:list --name=blog to inspect the generated routes.

Run module:make-inertia-page to add a page to an existing module.

Terminal window
php artisan module:make-inertia-page Posts/Index Blog

This writes Modules/Blog/resources/js/Pages/Posts/Index.vue (using inertia.frontend by default). Add --vue, --react, or --svelte to override the framework for that page, passing at most one flag.

Render the page from a controller with Inertia::render('Blog/Posts/Index'). Run npm run build or keep npm run dev running so Vite picks up the new file.

Terminal window
php artisan module:make-inertia-component PostCard Blog

This writes Modules/Blog/resources/js/Components/PostCard.vue. The extension follows inertia.frontend or a single --vue, --react, or --svelte flag.

Terminal window
php artisan module:make-controller PostController Blog --inertia

The generated resource methods render Blog/Index, Blog/Create, Blog/Show, and Blog/Edit. Update the page names in the controller if you want paths like Blog/Posts/Index, and generate those pages with module:make-inertia-page.

Terminal window
php artisan module:make-middleware HandleInertiaRequests Blog --inertia

This writes Modules/Blog/app/Http/Middleware/HandleInertiaRequests.php, extending Inertia\Middleware with $rootView = 'app'. Register it in bootstrap/app.php in place of App\Http\Middleware\HandleInertiaRequests so only one Inertia middleware runs in the web group.

bootstrap/app.php
->withMiddleware(function (Middleware $middleware): void {
$middleware->web(append: [
\Modules\Blog\Http\Middleware\HandleInertiaRequests::class,
]);
})