PSR-15 Adapter for InertiaJS

Overview

inertia-psr15

Before using this library, is important to know what is Inertia.js, what is it for and how it works, in 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, the usage after installation and configuration can be easy as:

// In some RequestHandlerInterface class

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

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 cherif/inertia-psr15

2- Add the inertia middleware to the middlewares pipeline:


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

// ...
// - $app->pipe('/files', $filesMiddleware);
$app->pipe(\Cherif\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 Webpack is recommended in order 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 fullpipe/twig-webpack-extension

a factory might be needed to configure the Webpack extension

5- Configure the templte to use Webpack extension and the Inertia Twig extension shipped with the adapter by apdating config/autoload/template.global.php and webpack.global.php like the following:



// template.global.php

declare(strict_types=1);

use Cherif\InertiaPsr15\Twig\InertiaExtension;
use Fullpipe\TwigWebpackExtension\WebpackExtension;

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


// webpack.global.php

declare(strict_types=1);

return [
    'webpack' => [
        'manifest_file' => dirname(__DIR__, 2) . '/public/build/manifest.json',
        'public_dir' => dirname(__DIR__, 2) . '/build',
    ]
];

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:

>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        {% webpack_entry_css 'build/app' %}
    head>
    <body>
        {{ inertia(page) }}
        {% webpack_entry_js 'build/runtime' %}
        {% webpack_entry_js 'build/app' defer %}
    body>
html>

The template uses Webpack extension (webpack_entry_css, webpack_entry_js) 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 Cherif\InertiaPsr15\Middleware\InertiaMiddleware;
use Cherif\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'
        ]);
    }
}
You might also like...
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

A PSR-15 middleware adapter for react/http

A PSR-15 middleware adapter for react/http Wraps PSR-15 middleware into coroutines using RecoilPHP making them usable within react/http as middleware.

This package adds artisan commands to create VueJS components and InertiaJS components.
This package adds artisan commands to create VueJS components and InertiaJS components.

Laravel Vue Commands This package adds artisan commands to create VueJS components and InertiaJS components. Installation You can install the package

Hassle-free Laravel + Inertiajs + Svelte + TailwindCSS Boilerplate
Hassle-free Laravel + Inertiajs + Svelte + TailwindCSS Boilerplate

Hassle-free Laravel + Inertiajs + Svelte + TailwindCSS Boilerplate

Laravel, react, and inertiajs for LevelUp Feel-IT 2021

How To Install hehe Clone the repository: Create your environment file: cp .env.example .env The app key is used to salt passwords. If you need to wor

Gerenciador de módulos para Laravel/InertiaJs + Vuejs

Neewton Neewton é um gerenciador de módulos para uma aplicação Laravel com InertiaJs e VueJs. Propósito Este projeto tem o objetivo de deixar uma apli

Demo and practice application with Laravel 8 and InertiaJS. (From laracasts course)
Demo and practice application with Laravel 8 and InertiaJS. (From laracasts course)

InertiaJS playground ⚽️ Started with the Laracasts: Build Modern Laravel Apps Using Inertia.js course and decided to share all my code here, I'll be a

🍟 A headles datatable with inertiajs
🍟 A headles datatable with inertiajs

🍟 A headles datatable with inertiajs

Running Laravel and React stacks together using Vite and InertiaJS on Docker.

Laravel-Vite-Docker Running Laravel and React stacks together using Vite and InertiaJS on Docker. Explore project's blog » Report Bug · Request Featur

Middleware for PHP built on top of PSR-7 and PSR-15

zend-stratigility Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-stratigility. From "Strata", Latin for "layer", and "ag

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

PSR Log - This repository holds all interfaces/classes/traits related to PSR-3.

PSR Log This repository holds all interfaces/classes/traits related to PSR-3. Note that this is not a logger of its own. It is merely an interface tha

PSR-7 and PSR-15 JWT Authentication Middleware

PSR-7 and PSR-15 JWT Authentication Middleware This middleware implements JSON Web Token Authentication. It was originally developed for Slim but can

PSR-7 and PSR-15 HTTP Basic Authentication Middleware

PSR-7 and PSR-15 Basic Auth Middleware This middleware implements HTTP Basic Authentication. It was originally developed for Slim but can be used with

Testing utilities for the psr/log package that backs the PSR-3 specification.

FIG - Log Test Testing utilities for the psr/log package that backs the PSR-3 specification. Psr\Log\Test\LoggerInterfaceTest provides a base test cla

⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

An Inertia.js adapter for Statamic.

Inertia.js adapter for Statamic Statamic server side adapter for Inertia.js to build single-page apps, without building an API. Installation You can i

Comments
  • Fix warning re. return type on PHP 8.1 upgrade

    Fix warning re. return type on PHP 8.1 upgrade

    Hey,

    I received this deprecation warning when using this library with PHP 8.1:

    Return type of Cherif\InertiaPsr15\Model\Page::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice
    

    As per this PR, I was able to fix it by adding a return type to the method in question.


    BTW thanks for writing this adapter and making it available! I really appreciate being able to use Inertia using PSR standards alone, without being tied to a specific framework.

    opened by matt-harvey 3
Releases(v0.1.0)
Owner
Mohamed Cherif Bouchelaghem
Maintaining @canjs
Mohamed Cherif Bouchelaghem
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.

?? LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.

Biao Xie 3.7k Dec 29, 2022
Pug Yii2 adapter

Yii 2 Pug (ex Jade) extension This extension provides a view renderer for Pug templates for Yii framework 2.0 applications. Support GutHub issues Inst

Pug PHP 8 Jun 17, 2022
Pug template engine adapter for Slim

Pug for Slim For details about the template engine see phug-lang.com Installation Install with Composer: composer require pug/slim Usage with Slim 3 u

Pug PHP 5 May 18, 2022
It validates PSR-7 messages (HTTP request/response) against OpenAPI specifications

NOTICE - THE PACKAGE HAS BEEN CONTRIBUTED TO THE PHP LEAGUE Go to https://github.com/thephpleague/openapi-psr7-validator This package is here for exis

Dmitry Lezhnev 167 Sep 29, 2022
PSR-7 HTTP message library

PSR-7 Message Implementation This repository contains a full PSR-7 message implementation, several stream decorators, and some helpful functionality l

Guzzle 7.6k Jan 4, 2023
Strict PSR-7 implementation used by the Slim Framework

Strict PSR-7 implementation used by the Slim Framework, but you may use it separately with any framework compatible with the PSR-7 standard.

Slim Framework 96 Nov 14, 2022
Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components

Spiral HTTP Application Skeleton Spiral Framework is a High-Performance PHP/Go Full-Stack framework and group of over sixty PSR-compatible components.

Spiral Scout 152 Dec 18, 2022
A set of PSR-7 object decorators providing useful convenience methods

Slim-Http Slim PSR-7 Object Decorators Installation It's recommended that you use Composer to install this library. $ composer require slim/http This

Slim Framework 136 Sep 29, 2022
A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

Woohoo Labs. 153 Sep 5, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022