| Install | |
|---|---|
composer require solophp/inertia |
|
| Latest Version: | v1.2.0 |
| PHP: | >=8.1 |
| License: | MIT |
| Last Updated: | Jan 20, 2025 |
| Links: | GitHub · Packagist |
A PSR-7 and PSR-15 compatible adapter for Inertia.js, enabling seamless server-side integration with any PHP framework that implements these standards.
composer require solophp/inertia
use Solo\Inertia;
$inertia = new Inertia(
rootTpl: '/path/to/root.php',
assetsVersion: '1.0.0',
js: '/dist/app.js',
css: '/dist/app.css'
);
use Solo\Inertia\InertiaMiddleware;
$middleware = new InertiaMiddleware($assetsVersion);
To render an Inertia page:
$response = $inertia->render(
$request,
$response,
'Pages/Users/Index',
[
'users' => $users,
'filters' => $filters
]
);
You can share data with all pages by setting the sharedProps request attribute:
$request = $request->withAttribute('sharedProps', [
'auth' => [
'user' => $currentUser,
],
'flash' => [
'message' => $flashMessage,
],
]);
These props will be automatically merged with page-specific props during rendering.
Create a root template (e.g., root.php):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="<?= $css ?>" rel="stylesheet">
<script src="<?= $js ?>" defer></script>
</head>
<body>
<div id="app" data-page='<?= $page ?>'></div>
</body>
</html>
The adapter automatically handles Inertia's partial reload feature through the X-Inertia-Partial-Data header, allowing for efficient partial page updates.
Asset versioning is supported through the assetsVersion parameter, helping with cache busting and ensuring clients always have the latest assets.
The adapter supports sharing common data across all pages through the sharedProps request attribute. This is useful for data that should be available everywhere, such as user authentication status or flash messages.
final to prevent inheritance issuesreadonly for better immutabilityThe middleware automatically adjusts response status codes for specific HTTP methods:
The middleware now accepts the assets version directly, removing the container dependency for better flexibility and testing.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open-sourced software licensed under the MIT license.