Skip to content

API reference

All classes are in the Crenspire\Inertia namespace.

Inertia

The main service. It keeps no per-request state.

php
final class Inertia

Constructor

php
public function __construct(
    ResponseFactoryInterface $responseFactory,
    StreamFactoryInterface $streamFactory,
    View\RootViewRendererInterface $rootViewRenderer,
    Version\VersionProviderInterface $version = new Version\StaticVersion(),
    ?Flash\FlashStoreInterface $flashStore = null,
    ?Ssr\GatewayInterface $ssrGateway = null,
    ?LoggerInterface $logger = null,
    array $sharedProps = [],
    bool $encryptHistory = false,
    bool $allErrors = false,
)

Rendering

MethodDescription
render(ServerRequestInterface $request, string $component, array $props = []): ResponseInterfaceJSON for Inertia visits, the root view otherwise
createPage(ServerRequestInterface $request, string $component, array $props = []): PageResolves props and returns the page object
getVersion(): stringThe current asset version

render() and createPage() throw InvalidArgumentException when the component name is empty.

Redirects

MethodDescription
redirect(string|UriInterface $url, int $status = 302): ResponseInterfaceRedirect with a Location header
back(ServerRequestInterface $request, string $fallback = '/', int $status = 302): ResponseInterfaceRedirect to the Referer header, or to $fallback
location(ServerRequestInterface $request, string|UriInterface $url): ResponseInterfaceFull page visit: 409 with X-Inertia-Location for Inertia visits, 302 otherwise

Immutable configuration

Each method returns a new instance.

MethodDescription
withSharedProps(array $props): selfAdds props shared with every page
withEncryptHistory(bool $encrypt = true): selfEnables or disables history encryption
withAllErrors(bool $allErrors = true): selfSends every validation message per field
withUrlResolver(callable $resolver): selfSets fn (ServerRequestInterface $request): string that returns the page URL

Request helpers

Static methods that return a new request. Pass that request on to the next handler or to render().

MethodDescription
isInertiaRequest(ServerRequestInterface $request): boolWhether the request has the X-Inertia header
share(ServerRequestInterface $request, string|array|ProvidesInertiaProperties $key, mixed $value = null): ServerRequestInterfaceShares props for this request
withErrors(ServerRequestInterface $request, array $errors, string $bag = 'default'): ServerRequestInterfaceAdds validation errors for this request
encryptHistory(ServerRequestInterface $request, bool $encrypt = true): ServerRequestInterfaceOverrides history encryption for this request
clearHistory(ServerRequestInterface $request): ServerRequestInterfaceClears history on this render

Prop factories

MethodReturns
optional(callable $callback)Prop\OptionalProp
defer(callable $callback, string $group = 'default', bool $rescue = false)Prop\DeferProp
merge(mixed $value)Prop\MergeProp
deepMerge(mixed $value)Prop\MergeProp
always(mixed $value)Prop\AlwaysProp
once(callable $callback)Prop\OnceProp
scroll(mixed $value, string $wrapper = 'data', ProvidesScrollMetadata|callable|null $metadata = null, string $pageName = 'page')Prop\ScrollProp

Constants

ConstantValue
SHARED_ATTRIBUTEinertia.shared
ERRORS_ATTRIBUTEinertia.errors
ENCRYPT_HISTORY_ATTRIBUTEinertia.encryptHistory
CLEAR_HISTORY_ATTRIBUTEinertia.clearHistory

Page

The resolved page object.

php
final class Page implements JsonSerializable
{
    public readonly string $component;
    public readonly array $props;
    public readonly string $url;
    public readonly string $version;
    public readonly array $metadata;

    public function toArray(): array;
    public function jsonSerialize(): array;
}

jsonSerialize() encodes empty props as an object.

Middleware

Middleware\InertiaMiddleware

php
public function __construct(Inertia $inertia, ResponseFactoryInterface $responseFactory)

PSR-15 middleware that applies the protocol rules. See Protocol support.

Middleware\XsrfTokenMiddleware

php
public function __construct(
    Yiisoft\Csrf\CsrfTokenInterface $token,
    string $csrfHeaderName = 'X-CSRF-Token',
    string $cookieName = 'XSRF-TOKEN',
    string $clientHeaderName = 'X-XSRF-TOKEN',
    string $cookiePath = '/',
    string $sameSite = 'Lax',
)

Requires yiisoft/csrf. See CSRF protection.

Props

All prop types are in the Crenspire\Inertia\Prop namespace.

ClassImplementsCreated by
AlwaysPropInertia::always()
OptionalPropIgnoreFirstLoad, OnceableInertia::optional()
DeferPropDeferrable, IgnoreFirstLoad, Mergeable, Onceable, RescuableInertia::defer()
MergePropMergeable, OnceableInertia::merge(), Inertia::deepMerge()
OncePropOnceableInertia::once()
ScrollPropDeferrable, MergeableInertia::scroll()

Mergeable methods

Available on DeferProp, MergeProp and ScrollProp.

MethodDescription
merge(): staticMerge on partial reloads
deepMerge(): staticMerge nested objects recursively
append(bool|string|array $path = true, ?string $matchOn = null): staticAppend at the root, at a path, or at several paths
prepend(bool|string|array $path = true, ?string $matchOn = null): staticPrepend at the root, at a path, or at several paths
matchOn(string|array $matchOn): staticKeys used to match existing items

Onceable methods

Available on OptionalProp, DeferProp, MergeProp and OnceProp.

MethodDescription
once(bool $value = true, BackedEnum|UnitEnum|string|null $as = null, DateTimeInterface|DateInterval|int|null $until = null): staticRemember the value on the client
as(BackedEnum|UnitEnum|string $key): staticShare the remembered value under a custom key
until(DateTimeInterface|DateInterval|int $delay): staticExpire after seconds, an interval, or at a time
fresh(bool $value = true): staticSend the value even if the client has it

Deferrable methods

Available on DeferProp and ScrollProp.

MethodDescription
defer(?string $group = null): staticLoad the prop after the page renders

Scroll metadata

php
final class ScrollMetadata implements ProvidesScrollMetadata
{
    public function __construct(
        string $pageName,
        int|string|null $previousPage = null,
        int|string|null $nextPage = null,
        int|string|null $currentPage = null,
    );

    public static function fromPaginator(mixed $paginator, string $pageName = 'page'): self;
}

fromPaginator() accepts yiisoft/data paginators and throws InvalidArgumentException for other values.

Prop providers

php
interface ProvidesInertiaProperty
{
    public function toInertiaProperty(PropertyContext $context): mixed;
}

interface ProvidesInertiaProperties
{
    /** @return iterable<string, mixed> */
    public function toInertiaProperties(RenderContext $context): iterable;
}
ContextProperties
PropertyContextstring $key (dot path), array $props (siblings), ServerRequestInterface $request
RenderContextstring $component, ServerRequestInterface $request

Flash

Flash\InertiaFlash

php
public function __construct(Flash\FlashStoreInterface $store)
MethodDescription
errors(array $errors, string $bag = 'default'): selfValidation errors for the next render
flash(string|array $key, mixed $value = null): selfData exposed as page.flash
clearHistory(): selfClear encrypted history on the next render
preserveFragment(): selfKeep the URL fragment across the next redirect

Flash stores

php
interface FlashStoreInterface
{
    public function set(string $key, mixed $value): void;
    public function get(string $key): mixed;
    public function pull(string $key): mixed;
}
ImplementationDescription
Flash\SessionFlashStoreUses Yiisoft\Session\SessionInterface
Flash\ArrayFlashStoreIn memory, for tests

View

View\RootViewRendererInterface

php
public function render(View\InertiaView $inertia, ServerRequestInterface $request): string;

View\PhpRootViewRenderer

php
public function __construct(string $template, array $parameters = [])

Renders a PHP template with $inertia, $request and the entries of $parameters. Throws RuntimeException when the template doesn't exist.

View\InertiaView

MemberDescription
Page $pageThe page object
?SsrResponse $ssrThe SSR result, if any
body(string $id = 'app'): stringPage script element and root element, or the SSR body
legacyBody(string $id = 'app'): stringRoot element with a data-page attribute, or the SSR body
head(): stringSSR head tags, or an empty string
pageJson(): stringPage JSON that is safe to embed in HTML

Versions

php
interface Version\VersionProviderInterface
{
    public function getVersion(): string;
}
ImplementationConstructor
Version\StaticVersion(string $version = '')
Version\CallbackVersion(callable $callback)
Version\ManifestVersion(string $manifestPath)

Vite

Vite\Vite

php
public function __construct(
    string $publicPath,
    string $buildDirectory = 'build',
    string $manifest = '.vite/manifest.json',
    ?string $devServerUrl = null,
    string $hotFile = 'hot',
    string $baseUrl = '/',
)
MethodDescription
tags(string|array $entries): stringScript, stylesheet and preload tags
reactRefresh(): stringReact Fast Refresh preamble in dev mode
asset(string $path): stringURL of a processed asset
isRunningHot(): boolWhether the dev server is used
getManifestPath(): stringAbsolute path of the manifest

tags() and asset() throw RuntimeException when the manifest or an entry is missing.

Server-side rendering

php
interface Ssr\GatewayInterface
{
    public function dispatch(Page $page, ServerRequestInterface $request): ?Ssr\SsrResponse;
}

Ssr\HttpGateway

php
public function __construct(
    ClientInterface $client,
    RequestFactoryInterface $requestFactory,
    StreamFactoryInterface $streamFactory,
    string $url = 'http://127.0.0.1:13714/render',
    array $except = [],
    bool $throwOnError = false,
    ?LoggerInterface $logger = null,
)

Ssr\SsrResponse

php
final class SsrResponse
{
    public readonly string $head;
    public readonly string $body;
}

Header defines constants for the protocol headers: INERTIA, VERSION, LOCATION, REDIRECT, PARTIAL_COMPONENT, PARTIAL_ONLY, PARTIAL_EXCEPT, RESET, ERROR_BAG, INFINITE_SCROLL_MERGE_INTENT, EXCEPT_ONCE_PROPS and PURPOSE.

Released under the MIT License.