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.jswith@viteand renders@inertia. With React, place@viteReactRefreshbefore@vitefornpm run dev.
Generate an Inertia module
Section titled “Generate an Inertia module”-
Choose the frontend framework for generated pages. The default is
vue.To use React or Svelte, publish the configuration and set
inertia.frontend. Ifconfig/modules.phpalready exists, edit the existing file.Terminal window php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider" --tag=configconfig/modules.php 'inertia' => ['frontend' => 'react', // vue, react or svelte],module:makereads this key and accepts no framework flag of its own. -
Create the module.
Terminal window php artisan module:make Blog --inertiaThe command generates
BlogControllerand four pages (Index,Create,Show, andEdit) inModules/Blog/resources/js/Pages. It skips Blade views. Combining--inertiawith--plainwrites no Inertia files. -
Publish the Inertia entry file so Vite resolves pages from every module.
Terminal window php artisan module:publish-inertia --vueTerminal window php artisan module:publish-inertia --reactTerminal window php artisan module:publish-inertia --sveltePass at most one framework flag, or omit it to use
inertia.frontend. The command writesresources/js/app.jsand asks before overwriting an existing file. Move any custom code out ofresources/js/app.jsfirst, then pass--forceto overwrite without a prompt.See
module:publish-inertiafor flag validation, exit codes, and how the published entry mounts the app. -
Register your framework plugin in the root
vite.config.js. Keep your existing plugins and options, and make sureresources/js/app.jsis inlaravel({ 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 restvue(),// in your existing laravel({...}) call: input must include the entry file// input: ['resources/css/app.css', 'resources/js/app.js'],vite.config.js import react from '@vitejs/plugin-react'// in defineConfig({ plugins: [...] }): add the plugin call, keep the restreact(),// in your existing laravel({...}) call: input must include the entry file// input: ['resources/css/app.css', 'resources/js/app.js'],vite.config.js import { svelte } from '@sveltejs/vite-plugin-svelte'// in defineConfig({ plugins: [...] }): add the plugin call, keep the restsvelte(),// in your existing laravel({...}) call: input must include the entry file// input: ['resources/css/app.css', 'resources/js/app.js'],You can ignore the
[vite-plugin-svelte] no Svelte config foundbuild message. -
Build the assets.
Terminal window npm run build
How a page name resolves
Section titled “How a page name resolves”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>.
Check the result
Section titled “Check the result”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.
Add a page
Section titled “Add a page”Run module:make-inertia-page to add a page to an existing module.
php artisan module:make-inertia-page Posts/Index BlogThis 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.
Add a component
Section titled “Add a component”php artisan module:make-inertia-component PostCard BlogThis writes Modules/Blog/resources/js/Components/PostCard.vue. The extension follows inertia.frontend or a single --vue, --react, or --svelte flag.
Add an Inertia controller
Section titled “Add an Inertia controller”php artisan module:make-controller PostController Blog --inertiaThe 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.
Add the Inertia middleware
Section titled “Add the Inertia middleware”php artisan module:make-middleware HandleInertiaRequests Blog --inertiaThis 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.
->withMiddleware(function (Middleware $middleware): void { $middleware->web(append: [ \Modules\Blog\Http\Middleware\HandleInertiaRequests::class, ]);})