| Install | |
|---|---|
composer require maskulabs/inertia-psr |
|
| Latest Version: | v3.0.0-beta3-1 |
| PHP: | ^8.5 |
A framework-agnostic, PSR-based server-side adapter for Inertia.js v3.
inertia-psr provides a PSR-friendly implementation of the Inertia server protocol for PHP applications. It is designed to stay closely aligned with the official Inertia.js v3 documentation:
This package is designed for PSR-based PHP applications and expects integrations around:
Install the package with Composer:
composer require maskulabs/inertia-psr
inertia-psr focuses on the server-side Inertia protocol and response generation.
It does not provide:
<?php
use MaskuLabs\InertiaPsr\InertiaInterface;
use Psr\Http\Message\ResponseInterface;
final readonly class DashboardAction
{
public function __construct(
private InertiaInterface $inertia,
) {}
public function __invoke(): ResponseInterface
{
return $this->inertia->render('Dashboard', [
'stats' => [
'users' => 120,
'sales' => 54,
],
]);
}
}
This returns an Inertia response for the Dashboard page component.
In a real application, you will typically also:
You can configure the root view globally:
<?php
$inertia->setRootView(__DIR__ . '/views/app.php');
You can also set it per response:
<?php
return $this->inertia
->render('Dashboard')
->rootView(__DIR__ . '/views/app.php');
Your root view is the HTML shell used for the initial browser visit.
It should:
Example:
<?php
/** @var array $page */
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My App</title>
<!-- Include frontend assets here:
use the built JavaScript file in production,
or @vite/client + your app entry script during development. -->
</head>
<body>
<script type="application/json" data-page="app">
<?php echo json_encode($page, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>
</script>
<div id="app"></div>
</body>
</html>
The exact asset tags depend on your frontend tooling and framework integration.
<?php
$inertia->share('app.name', 'My App');
$inertia->share('auth.user', [
'id' => 1,
'name' => 'Jane Doe',
]);
<?php
$inertia->version('build-hash');
<?php
$inertia->flash('success', 'Profile updated successfully.');
<?php
return $inertia->back();
<?php
return $inertia->location('/login');
The package supports advanced Inertia prop behavior, including:
always()optional()defer()merge()deepMerge()once()shareOnce()scroll()Example:
<?php
use MaskuLabs\InertiaPsr\Props\ScrollProp\ScrollMetadata;
return $inertia->render('Users/Index', [
'users' => $inertia->scroll(
value: fn() => [
['id' => 1, 'name' => 'Jane'],
['id' => 2, 'name' => 'John'],
],
metadata: new ScrollMetadata(
pageName: 'page',
previousPage: null,
nextPage: 2,
currentPage: 1,
),
),
'stats' => $inertia->defer(
fn() => ['active' => 42],
'sidebar',
),
'permissions' => $inertia->once(
fn() => ['users.read', 'users.write'],
),
]);
The package includes support for:
To use the full Inertia request / response flow correctly, register the package middleware in your PSR-15 middleware stack.
Framework-specific wiring may differ, so see the integration examples for concrete setups.
See the docs directory for complete integration examples.
Available examples:
MIT