| Install | |
|---|---|
composer require maskulabs/inertia-yii |
|
| Latest Version: | 1.0.0-beta1 |
| PHP: | ^8.5 |
Yii integration for maskulabs/inertia-psr.
inertia-yii provides the Yii-specific pieces needed to use Inertia.js v3 in Yii applications, including rendering, middleware, validation error handling, session / flash adapters, and Vite integration.
Official Inertia.js documentation:
Inertia implementationmaskulabs/inertia-psrcomposer.jsonInstall the package with Composer:
composer require maskulabs/inertia-yii
If you want a ready starting point instead of wiring everything manually, see maskulabs/inertia-app.
It is an application template based on yiisoft/app with Inertia.js integration already configured.
<?php
use MaskuLabs\InertiaPsr\InertiaInterface;
use MaskuLabs\InertiaPsr\Response\ResponseInterface;
final readonly class DashboardAction
{
public function __construct(
private InertiaInterface $inertia,
) {}
public function __invoke(): ResponseInterface
{
return $this->inertia->render('Dashboard', [
'stats' => [
'users' => 120,
'sales' => 54,
],
]);
}
}
The package ships with a default root view, but you can also configure your own.
Your root view should:
ShareMiddleware is intended to be configured in the Yii middleware stack.
Example:
<?php
use MaskuLabs\InertiaPsr\Middleware\InertiaMiddleware;
use MaskuLabs\InertiaYii\Middleware\ShareMiddleware as InertiaShareMiddleware;
return [
InertiaMiddleware::class,
[
'class' => InertiaShareMiddleware::class,
'withDefinitions()' => [
Reference::to(I18n::class),
Reference::to(Layout::class),
],
],
];
ValidationExceptionMiddleware catches validation exceptions and flashes formatted errors so they can be returned to the frontend after redirect.
Example:
<?php
use MaskuLabs\InertiaYii\Exception\ValidationException;
if (!$formHydrator->populateFromPostAndValidate($loginForm, $request)) {
throw new ValidationException($loginForm->getValidationResult());
}
ViteAsset supports two common modes:
.vite/manifest.jsonMIT