---
name: yii2-inertia-integration
description: Configure, troubleshoot, or migrate yii2-extensions/inertia in a Yii 2.0.55+ or Yii 22 application, including root views, props, validation flashes, signed CSRF cookies, redirects, and observers. Use for this PHP adapter; exclude Laravel adapters and frontend-only Inertia tasks.
license: BSD-3-Clause
metadata:
    source: https://github.com/yii2-extensions/inertia
---

# Integrate Inertia with Yii

Inspect the application's entry script, Composer requirements/lock, merged web configuration, request subclass, frontend
entry, and installed adapter/core versions. This adapter's 0.6 line requires PHP >=8.3, Yii `~2.0.55 || ^22.0@dev` in
the installed `composer.json`, and `php-forge/inertia ^0.5`; releases up to 0.6.2 require Yii 22. Do not claim
compatibility below Yii 2.0.55 or widen constraints as an installation workaround. Installing this skill does not
install the adapter, select a JavaScript framework, or build assets.

## Consult the relevant guide

In a root package checkout, use `composer show --self --format=json` for metadata. For installed dependencies, resolve
the real path and read the configured vendor directory's `composer/installed.php` (`versions[<package>]`):
`install_path`, `pretty_version` and Composer's selected `reference`. Do not blindly prefer `source.reference` over
`dist.reference`. For any live package checkout, including a symlinked path install, inspect its current `HEAD` and
files even when clean; report metadata mismatches and let the actual files govern. Uncommitted changes have no matching
SHA. Otherwise use the selected reference or a verified matching tag only after establishing its source provenance.
Compare `composer.lock`; it may differ from the inspected installation. Identify a lock-only state when no package files
are installed, and resolve dependencies separately. Use hosted pages, catalogs or snapshots only when their source
revision matches.

Prefer matching supplied Markdown or package `docs/`. A Composer archive may omit guides; a copied skill need not have a
sibling documentation tree. If local guides are unavailable, read the needed files at the resolved reference in the
[package repository](https://github.com/yii2-extensions/inertia):

- `installation.md`: dependencies, development-line resolution, bootstrap, and client prerequisites.
- `configuration.md`: manager defaults, root view encoding, flashes, CSRF, and observer/dispatcher contracts.
- `examples.md`: application root views and individual prop/redirect recipes.
- `migration.md` and root `UPGRADE.md`: upgrades from the original standalone implementation.
- `testing.md`: commands and the distinction between server, browser, and skill evidence.

Use the package's PHPDoc and installed core types for exact signatures. If a guide is not published at the installed
revision, state that gap and inspect the relevant source/changelog rather than substituting an unrelated adapter API.
The protocol and prop wrappers belong to `php-forge/inertia`; Yii response/session adaptation belongs here.

## Configure within the existing application

Preserve bootstrap entries, request settings, session handling, access rules, and application component IDs. Register
`yii\inertia\Bootstrap`; it registers `@inertia` and supplies a default `inertia` component when absent. Configure
`yii\inertia\Manager` under that ID. Return `Inertia::render()` or the adapter controller's protected `inertia()` helper
from actions, using exact client component names.

Start with the default root view or create the configured custom file before referencing it. Initial HTML must include
the matching root ID, HTML-safe `pageJson`, Yii's page lifecycle, and the application's own asset loading. The bundled
view targets Inertia v3 with a JSON script whose `data-page` value is the root ID. Older clients that read JSON from the
root element's `data-page` attribute need an attribute-encoded custom view; check the installed client instead of
assuming those formats are interchangeable. Encode ordinary text/attributes; emit the supplied `pageJson` directly only
inside its JSON script. Attribute-encode it when storing JSON in the root element's `data-page` attribute. The adapter
provides no Vite service, asset bundle, SSR runtime, or JavaScript package installer. Preserve the application's chosen
build; verify that its resolver can load the named component.

## Resolve props and form feedback

Prop callbacks take zero arguments; capture application state explicitly. A version closure may take the current Yii
request, unlike prop closures. Use facade factories for always, optional, deferred, merge, once, and scroll props; check
the installed core for fluent modifiers. `scrollMetadata()` creates metadata, not a prop wrapper.

Choose explicit serializable fields for models and identities. Page props can replace a whole shared top-level value; do
not promise recursive merging. Do not use `exposeSharedProps = false` to hide values: it disables key metadata. In a
long-lived worker, verify visitor isolation in the surrounding application lifecycle before sharing state.

Send validation errors through the configured session flash key (`errors` by default): string field keys mapped to
strings or lists of strings. They appear in `props.errors`; other flashes appear in top-level `flash`. Keep
`X-Inertia-Error-Bag` behavior when using form bags. Invalid error shapes throw explicitly. Version conflicts, prop
failures, and observer failures preserve flashes; a later view failure can happen after they are consumed.

## Keep CSRF and response handling intact

Use `yii\inertia\web\Request` for signed cookie-to-header CSRF, reconciling any existing subclass. Preserve the stable
private cookie validation key, cookie validation, and CSRF protection. The names are `XSRF-TOKEN` and `X-XSRF-TOKEN`.
Verify initial cookie creation (the bundled root does not call `getCsrfToken()`) and the actual same-origin
header/cookie exchange; do not disable validation to fix a `400` or replace it with a raw-token recipe from another
framework.

Return Yii redirects after writes and retain bootstrap's before-send normalization. Test PUT/PATCH/DELETE `302`
redirects becoming `303`. For external visits use `Inertia::location()` and check the `409`/`X-Inertia-Location`
response for Inertia requests. Fragment redirects use `X-Inertia-Redirect`, requiring a client that supports it (Inertia
v3). Preserve `Vary: X-Inertia`, version negotiation, and location headers through caches.

Configure a PSR-14 `eventDispatcher` before the manager creates its default protocol. A supplied `protocol` keeps its
own dispatcher. `pageObserver` receives resolved initial/JSON pages, skips version conflicts, and propagates callback
failures. Select/redact diagnostics before recording payloads. Debugger collector/panel setup belongs to that debugger's
installed version; the dispatcher option alone does not register them.

## Intended behavior

These look like bugs and are adapter contracts; do not "fix" them in package or application code:

- `Bootstrap` attaches response normalization only when the request already carries `X-Inertia` while the application
  bootstraps. Without it in `bootstrap`, no default `inertia` component exists and an Inertia PUT/PATCH/DELETE `302`
  stays `302`. Redirects of ordinary browser requests are never converted.
- `Manager::isInertiaRequest()` accepts only `true` or `1` in `X-Inertia`, trimmed and case-insensitive.
- With cookie validation enabled, `X-XSRF-TOKEN` must carry the signed `XSRF-TOKEN` cookie value. A masked token from
  `getCsrfToken()` sent in that header is rejected. The cookie is not HTTP-only so the client can read it.
- An `errors` flash with another shape throws `InvalidConfigException`; an empty field key throws the core
  `InvalidPageInputException`. The rejected flash stays available in the current request and expires on the next one. A
  `409` version conflict leaves every flash unconsumed.
- `share('user.name', ...)` builds a nested shared array, but a page prop named `user` replaces the whole shared `user`.
- `getVersion()` returns an empty string when no version is configured. `Inertia::location()` answers an ordinary
  request with a `302` redirect and an Inertia request with `409` and `X-Inertia-Location`.
- The core calls prop closures without arguments: a closure with a required parameter fails with the core
  `PropResolutionException`, unless a deferred prop with `rescue()` enabled absorbs the failure. Optional and variadic
  parameters resolve normally.

`Exception\Message` holds the adapter's exception texts; read it only to match an error message.

## Migrate and verify

For 0.1 consumers, replace removed `yii\inertia\Page` and prop classes with core types or facade factories, remove
`yii\inertia\Vite` usage, and review top-level flash access and callback contracts. Check the migration guide against
the actual starting version rather than treating every upgrade as a new API break.

Run the consuming application's existing checks. Verify HTML, JSON, a matching partial reload, deferred resolution, a
valid/invalid CSRF submission, write redirects, validation flashes, and an outdated asset version. Native client
mounting and skill activation require their own evidence. Report exact versions, checks performed, and gaps; a syntax
check or manual skill read does not prove those outcomes.
