Skip to content

Yii3 InertiaModern single-page apps with Yii3

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.

Yii3 Inertia

Quick look

Return a page component and its props from a Yii3 action. Expensive data can load after the page renders:

php
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:

jsx
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:

bash
composer require crenspire/yii3-inertia:^2.0

Released under the MIT License.