Server-Side Setup
The first step when installing Inertia is to configure your server-side framework. Inertia maintains an official server-side adapter for Laravel. For other frameworks, please see the community adapters.
Inertia is fine-tuned for Laravel, so the documentation examples on this website utilize Laravel. For examples of using Inertia with other server-side frameworks, please refer to the framework specific documentation maintained by that adapter.
Installation
1. Install dependencies
First, install the Inertia server-side adapter using the Composer package manager.
1composer require inertiajs/inertia-laravel1composer require inertiajs/inertia-laravel
2. Setup root template
Next, create a resources/views/app.blade.php file. This root template will be loaded on the first page visit to your application, and should include your assets along with the @inertia and @inertiaHead directives.
1<html>2 <head>3 <meta charset="utf-8" />4 <meta name="viewport" content="width=device-width, initial-scale=1">5 @vite('resources/js/app.js')6 @inertiaHead7 </head>8 <body>9 @inertia10 </body>11</html>1<html>2 <head>3 <meta charset="utf-8" />4 <meta name="viewport" content="width=device-width, initial-scale=1">5 @vite('resources/js/app.js')6 @inertiaHead7 </head>8 <body>9 @inertia10 </body>11</html>
For React applications, it's recommended to include the @viteReactRefresh directive before the @vite directive to enable Fast Refresh in development.
The @inertia directive renders a <div> element with an id of app. This element serves as the mounting point for your JavaScript application. You may customize the id by passing a different value to the directive.
1<html>2 ...3 <body>4 @inertia('custom-app-id')5 </body>6</html>1<html>2 ...3 <body>4 @inertia('custom-app-id')5 </body>6</html>
If you change the id of the root element, be sure to update it client-side as well.
By default, Inertia's Laravel adapter will assume your root template is named app.blade.php. If you would like to use a different root view, you can change it using the Inertia::setRootView() method.
3. Register middleware
Next we need to setup the Inertia middleware. You can accomplish this by publishing the HandleInertiaRequests middleware to your application, which can be done using the following Artisan command.
1php artisan inertia:middleware1php artisan inertia:middleware
Once the middleware has been published, append the HandleInertiaRequests middleware to the web middleware group in your application's bootstrap/app.php file.
1use App\Http\Middleware\HandleInertiaRequests;23->withMiddleware(function (Middleware $middleware) {4 $middleware->web(append: [5 HandleInertiaRequests::class,6 ]);7})1use App\Http\Middleware\HandleInertiaRequests;23->withMiddleware(function (Middleware $middleware) {4 $middleware->web(append: [5 HandleInertiaRequests::class,6 ]);7})
This middleware provides a version() method for setting your asset version, as well as a share() method for defining shared data.
4. Create your first response
That's it, you're all ready to go server-side! Now you're ready to start creating Inertia pages and rendering them via responses.
1use Inertia\Inertia;23class EventsController extends Controller4{5 public function show(Event $event)6 {7 return Inertia::render('Event/Show', [8 'event' => $event->only(9 'id',10 'title',11 'start_date',12 'description',13 ),14 ]);15 }16}1use Inertia\Inertia;23class EventsController extends Controller4{5 public function show(Event $event)6 {7 return Inertia::render('Event/Show', [8 'event' => $event->only(9 'id',10 'title',11 'start_date',12 'description',13 ),14 ]);15 }16}