The first step when installing Inertia is to configure your server-side framework. Inertia ships with official server-side adapters for Laravel and Rails. For other frameworks, please see the community adapters.
Install the Inertia server-side adapters using the preferred package manager for that language or framework.
composer require inertiajs/inertia-laravel
Next, setup the root template that will be loaded on the first page visit. This will be used to load your site assets (CSS and JavaScript), and will also contain a root <div>
to boot your JavaScript application in.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<link href="{{ mix('/css/app.css') }}" rel="stylesheet" />
<script src="{{ mix('/js/app.js') }}" defer></script>
@inertiaHead
</head>
<body>
@inertia
</body>
</html>
Next, setup the Inertia middleware. In the Rails adapter, this is configured automatically for you. However, in Laravel you need to publish the HandleInertiaRequests
middleware to your application, which can be done using this artisan command:
php artisan inertia:middleware
Once generated, register the HandleInertiaRequests
middleware in your App\Http\Kernel
, as the last item in your web
middleware group.
'web' => [
// ...
\App\Http\Middleware\HandleInertiaRequests::class,
],
This middleware provides a version()
method for setting your asset version, and a share()
method for setting shared data. Please see those pages for more information.
That's it, you're all ready to go server-side! From here you can start creating Inertia responses. See the responses page for more information.
use Inertia\Inertia;
class EventsController extends Controller
{
public function show(Event $event)
{
return Inertia::render('Event/Show', [
'event' => $event->only(
'id',
'title',
'start_date',
'description'
),
]);
}
}