onix-systems-php / hyperf-inertia

MIT License
2 stars 1 forks source link

Inertia.js Hyperf Adapter


Visit inertiajs.com to learn more.

Todo

Installation

Middleware

Next we need to setup the Inertia middleware, in file ./config/middlewares.php . You can accomplish this by publishing the HandleInertiaMiddleware middleware to your application.

Note This middleware must be placed before the validation middleware.

'http' => [
\App\Common\Middleware\HandleInertiaMiddleware::class,
],

Creating responses

    #[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',
            ],
        ]);
    }

Frontend Part

After executing php ./bin/hyperf.php inertia:init you will get the folder structure.

.
├── storage
│   ├── inertia
│   │    ├── css
│   │    │  └── app.css
│   │    └── js
│   │       ├── Components  
│   │       ├── Layouts  
│   │       ├── Pages
│   │       ├── app.js
│   │       ├── sst.js (optional)
│   │       └── bootstrap.js
│   ├── view
│   │    └── layouts 
│   │       └── app.blade.php
│   └── ...
├── vite.config.json
└── ... 

In folders Components, Layouts, Pages you can create your own components, layouts and pages.

Ssr

In the ./bin/hyperf.php inertia:init process, you can select ssr. If you have set ssr separately, you can copy this file from hyperf-inertia/src/Commands/Stubs/{your_framework}/ssr.{ext} Or read the inertia documentation .

How to run ssr

Environment variables

Examples of frontend files

In the package you can find example files for the frontend. vendor/onix-systems-php/hyperf-inertia/example There is also an example controller here.

Note If you are using arm64, you need to add @rollup/rollup-darwin-arm64, this is only for svelte