crenspire / yii3-inertia
Inertia.js server-side adapter for Yii3 and any PSR-15 application
Requires
- php: 8.2 - 8.5
- psr/http-factory: ^1.1
- psr/http-message: ^1.1 || ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- psr/log: ^2.0 || ^3.0
Requires (Dev)
- nyholm/psr7: ^1.8.2
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- psr/http-client: ^1.0
- yiisoft/aliases: ^3.1
- yiisoft/csrf: ^2.2
- yiisoft/data: ^2.0
- yiisoft/di: ^1.4
- yiisoft/session: ^3.0
Suggests
- psr/http-client: To use the HTTP server-side rendering gateway
- yiisoft/aliases: To use aliases such as @root in the package params
- yiisoft/csrf: To use XsrfTokenMiddleware with the Inertia.js client's X-XSRF-TOKEN header
- yiisoft/data: To build infinite scroll props from Yii data paginators
- yiisoft/session: To flash validation errors, flash data and history flags across redirects
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-17 04:09:06 UTC
README
Inertia.js adapter for Yii3
Documentation · Installation · API reference · Upgrade from 1.x
A server-side Inertia.js adapter for Yii3 and any other PSR-7/PSR-15 application. Build single-page apps with React, Vue or Svelte while keeping routing, controllers and validation in PHP.
Features
- Inertia.js 3 protocol, with support for Inertia.js 1 and 2 clients
- Partial reloads, and deferred, optional, always, merge, once and infinite scroll props
- Validation errors with error bags, flash data and history encryption
- CSRF protection for
yiisoft/csrf - Vite tags for the dev server and production builds, with automatic asset versioning
- Server-side rendering through a PSR-18 HTTP client
- Zero-config setup in Yii3 through
yiisoft/config - Stateless services, safe for RoadRunner, Swoole and FrankenPHP workers
Requirements
- PHP 8.2 – 8.5
- A PSR-17 HTTP factory implementation
Installation
composer require crenspire/yii3-inertia:^2.0
In a Yii3 application, the services are registered automatically. Add the middleware before the router in
config/web/di/application.php:
SessionMiddleware::class, \Crenspire\Inertia\Middleware\XsrfTokenMiddleware::class, // when using yiisoft/csrf CsrfTokenMiddleware::class, \Crenspire\Inertia\Middleware\InertiaMiddleware::class, Router::class,
Create the root view at resources/views/inertia.php:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title data-inertia>My App</title> <?= $vite->reactRefresh() ?> <?= $vite->tags('resources/js/app.jsx') ?> <?= $inertia->head() ?> </head> <body> <?= $inertia->body() ?> </body> </html>
Then set up the frontend as described in Client-side setup.
Usage
use Crenspire\Inertia\Inertia; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; final readonly class IndexAction { public function __construct( private Inertia $inertia, private UserRepository $users, ) {} public function __invoke(ServerRequestInterface $request): ResponseInterface { return $this->inertia->render($request, 'Users/Index', [ 'users' => fn () => $this->users->findAll(), 'stats' => Inertia::defer(fn () => $this->users->statistics()), ]); } }
import { Deferred } from '@inertiajs/react' export default function Index({ users, stats }) { return ( <> <UserTable users={users} /> <Deferred data="stats" fallback={<p>Loading…</p>}> <Stats stats={stats} /> </Deferred> </> ) }
Read the documentation for forms and validation, shared data, all prop types, Vite, server-side rendering, usage without Yii3, and more.
Examples
examples/psr15: a runnable application built only from PSR componentsexamples/yii3: files for an application created fromyiisoft/app
Upgrading from 1.x
Version 2.0 is a rewrite with a new API. See the upgrade guide.
Contributing
See CONTRIBUTING.md. The documentation lives in docs and is published with GitHub
Pages.
License
MIT. See LICENSE.