定制 sirix/inertia-psr15 二次开发

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

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

sirix/inertia-psr15

最新稳定版本:1.1.1

Composer 安装命令:

composer require sirix/inertia-psr15

包简介

InertiaJS server-side adapter for PSR-7 and PSR-15 applications

README 文档

README

This is a maintained fork of https://github.com/cherifGsoul/inertia-psr15. It preserves the original goal while updating PHP versions, tooling, and namespace/package details for this fork. See “Fork notes” below for differences.

Before using this library, it’s important to know what is Inertia.js, what is it for and how it works, on the official Inertia.js website.

PHP PSR-15 InertiaJS server-side adapter, it can be used with Mezzio, Slim or any framework that implements PSR-15 interfaces.

The adapter is a PSR-15 middleware to detect InertiaJS requests and prepare and send Response to be read and rendered by InertiaJS front-end components. After installation and configuration, usage can be as simple as:

// In some RequestHandlerInterface class

$inertia = $request->getAttribute(InertiaMiddleware::INERTIA_ATTRIBUTE);
return $inertia->render('MyFrontEndComponent', [
    'someProp' => 'someProp Prop Value',
    'ohterProp' => 'ohterProp Prop Value'
]);

Fork notes

  • Package name: sirix/inertia-psr15
  • PHP versions: ~8.1 to ~8.4 supported
  • Namespace: Sirix\InertiaPsr15\
  • Tooling added: PHP-CS-Fixer, PHPStan, Rector configs and GitHub Actions.
  • This README has been adjusted to reflect the fork; code usage remains the same with updated namespaces.

Usage:

A small application was made to demonstrate how this adapter can be used in Mezzio application.

The adapter is designed to work with Mezzio with little effort, in the following we assume that a Mezzio application was generated using Mezzio Skeleton and Twig was selected as the template engine:

Installation:

1- Install the adapter:

composer require sirix/inertia-psr15

2- Add the inertia middleware to the middlewares pipeline:

<?php
//my-mezzio-app/config/pipeline.php

// ...
// - $app->pipe('/files', $filesMiddleware);
$app->pipe(\Sirix\InertiaPsr15\Middleware\InertiaMiddleware::class);

// Register the routing middleware in the middleware pipeline.
// This middleware registers the Mezzio\Router\RouteResult request attribute.
$app->pipe(RouteMiddleware::class);

// ...

3- Please refer to InertiaJS to install a client-side adapter.

4- Using Vite is recommended to build the front-end application, however, to render the built JS/CSS assets in a Twig template, the following extension can be used:

composer require sirix/twig-vite-extension

a factory might be needed to configure the Vite extension

5- Configure the template to use Vite extension and the Inertia Twig extension shipped with the adapter by updating config/autoload/template.global.php and vite.global.php like the following:

<?php

// template.global.php

declare(strict_types=1);

use Sirix\InertiaPsr15\Twig\InertiaExtension;
use Sirix\TwigViteExtension\Twig\ViteExtension;

return [
    'templates' => [
        'paths' => [
            'error'    => [dirname(__DIR__, 2) . '/templates/error'],
            '__main__' => [dirname(__DIR__, 2) . '/templates'],
        ],
    ],
    'twig'      => [
        'extensions' => [
            ViteExtension::class,
            InertiaExtension::class,
        ],
    ],
];
<?php

// vite.global.php

declare(strict_types=1);

return [
    'vite' => [
        'options' => [
            'is_dev_mode'      => true,
            'vite_build_dir'   => 'public/build',
            'vite_public_base' => 'build',
            'dev_server'       => 'http://localhost:5173',
        ],
    ],
];

6- The adapter needs just one backend template to render the application, and by default it will look for templates/app.html.twig if a default template is not configured, the app template can be like the following:

<!DOCTYPE html>
<html lang="en" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block title %}Mezzio with Inertia.js{% endblock %}</title>
    {{ vite_entry_link_tags('resources/js/app.ts') }}
</head>
<body class="font-sans antialiased">
{% block body %}
{{ inertia(page) }}
{% endblock %}

{{ vite_entry_script_tags('resources/js/app.ts') }}
</body>
</html>

The template uses Vite extension (vite_entry_link_tags) to render the assets and Inertia extension inertia(page) to mount the front-end application.

After successful configuration, the adapter can be used to render the front-end component instead of the HTML templates:

declare(strict_types=1);

namespace App\Handler;

use Sirix\InertiaPsr15\Middleware\InertiaMiddleware;
use Sirix\InertiaPsr15\Service\InertiaInterface;
use Mezzio\LaminasView\LaminasViewRenderer;
use Mezzio\Plates\PlatesRenderer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class HomePageHandler implements RequestHandlerInterface
{
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        /** @var InertiaInterface $inertia */
        $inertia = $request->getAttribute(InertiaMiddleware::INERTIA_ATTRIBUTE);
        return $inertia->render('Home', [
            'greeting' => 'Hello Inertia PSR-15'
        ]);
    }
}

Copyright

Original author: Mohammed Cherif BOUCHELAGHEM (2021) Fork maintainer: Sirix (2025)

License: BSD-3-Clause (see LICENSE)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-08-30