Inertia.js 3 protocol
Partial reloads, deferred, optional, merge, once and infinite scroll props, with support for Inertia.js 1 and 2 clients.
The Inertia.js server-side adapter for Yii3 and any PSR-15 application. Use React, Vue or Svelte while routing, controllers and validation stay in PHP.
Return a page component and its props from a Yii3 action. Expensive data can load after the page renders:
use Crenspire\Inertia\Inertia;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Router\CurrentRoute;
final readonly class ShowAction
{
public function __construct(
private Inertia $inertia,
private UserRepository $users,
) {}
public function __invoke(ServerRequestInterface $request, CurrentRoute $route): ResponseInterface
{
$id = (int) $route->getArgument('id');
return $this->inertia->render($request, 'Users/Show', [
'user' => $this->users->get($id),
'activity' => Inertia::defer(fn () => $this->users->activity($id)),
]);
}
}The React page receives the props. <Deferred> shows a fallback until activity arrives:
import { Deferred, Head } from '@inertiajs/react'
export default function Show({ user, activity }) {
return (
<>
<Head title={user.name} />
<h1>{user.name}</h1>
<Deferred data="activity" fallback={<p>Loading activity…</p>}>
<ActivityList items={activity} />
</Deferred>
</>
)
}Ready to start? Install the package and follow the installation guide:
composer require crenspire/yii3-inertia:^2.0