onix-systems-php / hyperf-inertia
Hyperf 对 Inertia.js 的适配器。
v1.0.9
2024-07-11 09:59 UTC
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- hyperf/collection: ^3.1
- hyperf/command: ^3.1
- hyperf/config: ^3.1
- hyperf/guzzle: ^3.1
- hyperf/resource: ^3.1
- hyperf/session: ^3.1
- hyperf/stringable: ^3.1
- hyperf/view-engine: ^3.1
- symfony/process: ^6.4 | ^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- hyperf/testing: ^3.1
- mockery/mockery: ^1.3.3
- roave/security-advisories: dev-master
Suggests
- ext-pcntl: Recommended when running the Inertia SSR server via the `inertia:start-ssr` artisan command.
This package is auto-updated.
Last update: 2024-09-22 11:10:59 UTC
README
访问 inertiajs.com 了解更多信息。
待办事项
- 以编程方式初始化 node_modules。
安装
composer require onix-systems-php/hyperf-inertia
安装包。php ./bin/hyperf.php vendor:publish onix-systems-php/hyperf-inertia
发布配置和中间件。npm init
如果不存在,则初始化 package.jsonphp ./bin/hyperf.php inertia:init
初始化 inertia。- 将命令添加到 package.json
"scripts": { "dev": "vite", "build": "vite build", "build-ssr": "vite build && vite build --ssr" (optional) },
npm i
安装 node_modules 包- 运行 inertia
npm run dev
用于开发npm run build
用于生产
注意 如果您使用 Svelte,package.json 类型必须是模块。
"type": "module"
中间件
接下来,我们需要设置 Inertia 中间件,在文件 ./config/middlewares.php
中。您可以通过将 HandleInertiaMiddleware 中间件发布到您的应用程序来完成此操作。
注意 此中间件必须放在
validation
中间件之前。
'http' => [ \App\Common\Middleware\HandleInertiaMiddleware::class, ],
创建响应
#[GetMapping(path: '/test-feedback')] public function test(RequestInterface $request) { return inertia('Test', [ 'title' => 'Feedback form', 'formTitle' => "What's on your mind?" ])->toResponse($request); }
#[PostMapping('/test-feedback')] public function testFeedback(RequestInterface $request) { $rules = [ 'fullname' => 'required|string|min:3|max:255', 'email' => 'required|email', 'message' => 'required|string|min:10|max:255', ]; $validator = $this->validationFactory->make($request->all(), $rules); $validator->validate(); return redirect_with('test-feedback', [ 'alert' => [ 'message' => 'Feedback was successfully sent', 'type' => 'success', ], ]); }
前端部分
执行 php ./bin/hyperf.php inertia:init
后,您将获得文件夹结构。
.
├── storage
│ ├── inertia
│ │ ├── css
│ │ │ └── app.css
│ │ └── js
│ │ ├── Components
│ │ ├── Layouts
│ │ ├── Pages
│ │ ├── app.js
│ │ ├── sst.js (optional)
│ │ └── bootstrap.js
│ ├── view
│ │ └── layouts
│ │ └── app.blade.php
│ └── ...
├── vite.config.json
└── ...
在 Components
、Layouts
、Pages
文件夹中,您可以创建自己的组件、布局和页面。
SSR
在 ./bin/hyperf.php inertia:init
过程中,您可以选择 ssr。如果您已单独设置 ssr,则可以从 hyperf-inertia/src/Commands/Stubs/{your_framework}/ssr.{ext}
复制此文件或阅读 inertia 文档。
如何运行 ssr
npm run build-ssr
- 将
php ./bin/hyperf.php inertia:start-ssr
添加到容器运行命令以启动 ssr 服务器。
环境变量
INERTIA_SSR_ENABLED
- 如果您使用 ssr,则设置为 true。默认为 false。INERTIA_SSR_URL
- ssr 服务器的 URL。默认为 http://127.0.0.1:13714。INERTIA_IS_SECURE
- 如果您使用 http,则设置为 false。默认为 true。INERTIA_SKIP_URL_PREFIX
- 如果您需要跳过 URL,则设置为前缀。前缀应使用分隔符,
指定为一个字符串。默认为空字符串。例如v1,api
。所有以此前缀开始的请求都将被惯性跳过。INERTIA_NO_SKIP_EXTRA_PATH
当前缀存在于INERTIA_SKIP_URL_PREFIX
中时,但必须为单独的路径做出例外。额外的路径应使用分隔符,
指定为一个字符串。默认为空字符串。例如admin/login,admin/forgot
。
前端文件示例
在包中可以找到前端示例文件。vendor/onix-systems-php/hyperf-inertia/example。这里也有一个示例控制器。
- 在 inertia.blade.php 中连接样式
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
注意 如果您使用 arm64,则需要添加
@rollup/rollup-darwin-arm64
,这仅适用于 svelte