定制 yii2-framework/inertia 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yii2-framework/inertia

最新稳定版本:0.1.0

Composer 安装命令:

composer require yii2-framework/inertia

包简介

Inertia js server-side integration layer for yii2.

README 文档

README

Yii Framework

Inertia


PHPUnit Mutation Testing PHPStan

Inertia.js server-side integration layer for Yii2
Server-driven pages, shared props, redirects, and asset version handling without jQuery

Features

Feature Overview

Overview

yii2-extensions/inertia is the server-side base package for building modern Inertia-driven pages on top of Yii2. It does not ship a client adapter. Instead, it defines the server contract that future packages such as yii2-extensions/inertia-vue, yii2-extensions/inertia-react, and yii2-extensions/inertia-svelte can reuse.

Installation

composer require yii2-extensions/inertia:^0.1

Register the bootstrap class in your application configuration:

return [
    'bootstrap' => [\yii\inertia\Bootstrap::class],
];

Quick start

Render a page directly from a controller action:

use yii\inertia\Inertia;
use yii\web\Controller;
use yii\web\Response;

final class SiteController extends Controller
{
    public function actionIndex(): Response
    {
        return Inertia::render(
            'Dashboard',
            ['stats' => ['visits' => 42]],
        );
    }
}

Or extend the convenience controller:

use yii\inertia\web\Controller;
use yii\web\Response;

final class SiteController extends Controller
{
    public function actionIndex(): Response
    {
        return $this->inertia(
            'Dashboard',
            [
                'stats' => ['visits' => 42],
            ]
        );
    }
}

Or inject the renderer contract. Bootstrap binds ResponseRendererInterface to InertiaRenderer in the DI container, so the action stays decoupled from the Inertia implementation and overlays (Inertia, JSON, API) can swap the renderer without rewriting controller code:

use yii\inertia\web\ResponseRendererInterface;
use yii\web\{Controller, Response};

final class SiteController extends Controller
{
    public function __construct(
        $id,
        $module,
        private readonly ResponseRendererInterface $renderer,
        $config = [],
    ) {
        parent::__construct($id, $module, $config);
    }

    public function actionIndex(): Response|string
    {
        return $this->renderer->render('Dashboard', ['stats' => ['visits' => 42]]);
    }
}

Configuration example

use yii\inertia\Manager;

return [
    'bootstrap' => [\yii\inertia\Bootstrap::class],
    'components' => [
        'inertia' => [
            'class' => Manager::class,
            'id' => 'app',
            'rootView' => '@app/views/layouts/inertia.php',
            'version' => static function (): string {
                $path = dirname(__DIR__) . '/public/build/manifest.json';

                return is_file($path) ? (string) filemtime($path) : '';
            },
            'shared' => ['app.name' => static fn(): string => Yii::$app->name],
        ],
    ],
];

Dev server support (Vite)

The bundled \yii\inertia\Vite helper component renders asset tags for both development and production. When devMode is true, it emits @vite/client plus each configured entrypoint from devServerUrl, enabling Vite's HMR WebSocket end-to-end. When devMode is false, it reads the manifest at manifestPath and renders hashed asset tags for production.

Typical development flow: run npm run dev to start the Vite dev server and launch Yii2 with a dev environment flag (for example, YII_ENV=dev ./yii serve). YII_ENV does not toggle Vite::$devMode on its own; your application configuration must wire the two together, for example:

'components' => [
    'inertiaVite' => [
        'class' => \yii\inertia\Vite::class,
        'devMode' => YII_ENV === 'dev',
        // ...
    ],
],

For framework-specific setup, see:

Prop types (v3)

The package supports the Inertia v3 prop types for fine-grained control over when and how props are resolved:

use yii\inertia\Inertia;

return Inertia::render(
    'Dashboard',
    [
        'stats' => $stats,                                              // regular prop
        'users' => Inertia::defer(fn () => User::find()->all()),        // loaded after render
        'activity' => Inertia::optional(fn () => $user->getActivity()), // only on partial reload
        'auth' => Inertia::always(fn () => ['user' => $identity]),      // always included
        'items' => Inertia::merge($paginated)->append('data', 'id'),    // merge instead of replace
        'countries' => Inertia::once(fn () => Country::find()->all()),  // resolved once, cached
    ],
);

See the Usage Examples for detailed documentation on each prop type.

Validation and flash messages

This package maps the session flash key errors to props.errors and exposes all remaining flashes at the top-level flash page key. A typical validation redirect looks like this:

if (!$model->validate()) {
    Yii::$app->session->setFlash('errors', $model->getErrors());

    return $this->redirect(['create']);
}

Yii::$app->session->setFlash('success', 'Record created.');

return $this->redirect(['view', 'id' => $model->id]);

CSRF protection

Drop in yii\inertia\web\Request to enable Inertia's automatic cookie-to-header CSRF flow:

'request' => [
    'class' => \yii\inertia\web\Request::class,
    'cookieValidationKey' => 'your-secret-key',
],

Inertia's HTTP client reads the XSRF-TOKEN cookie and sends it as X-XSRF-TOKEN automatically no client-side configuration required. See the Configuration Reference for details.

Package boundaries

This repository intentionally does not include Vue, React, or Svelte bootstrapping. Those concerns belong in separate client adapter packages built on top of the server contract defined here.

Documentation

For detailed configuration options and advanced usage.

Package information

PHP Yii 22.0.x Latest Stable Version Total Downloads

Quality code

Codecov PHPStan Level Max Super-Linter StyleCI

License

License

yii2-framework/inertia 适用场景与选型建议

yii2-framework/inertia 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.26k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2026 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「yii2」 「SPA」 「inertia」 「server-driven」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yii2-framework/inertia 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yii2-framework/inertia 我们能提供哪些服务?
定制开发 / 二次开发

基于 yii2-framework/inertia 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.26k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-04-03