# inertia-psr15
Before using this library, is important to know [what is Inertia.js](https://inertiajs.com/#top), [what is it for](https://inertiajs.com/who-is-it-for) and [how it works](https://inertiajs.com/how-it-works), in the [official Inertia.js website](https://inertiajs.com/)
PHP PSR-15 [InertiaJS](https://inertiajs.com/) server-side adapter, it can be used with [Mezzio](https://mezzio.dev/), [Slim](https://www.slimframework.com/) 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:
```php
// 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](https://github.com/cherifGsoul/mezzio-inertia-demo) was made to demonstrate how this adapter can be used in Mezzio application.
The adapter is designed to work with [Mezzio](https://mezzio.dev/) with little effort, in the following we assume that
a Mezzio application was generated using [Mezzio Skeleton](https://github.com/mezzio/mezzio-skeleton) and Twig
was selected as the template engine:
### Installation:
1- Install the adapter:
```shell
composer require cherif/inertia-psr15
```
2- Add the inertia middleware to the middlewares pipeline:
```php
<?php
//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](https://inertiajs.com/client-side-setup) 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:
```shell
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:
```php
<?php
// 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
]
]
];
```
```php
<?php
// 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
<!DOCTYPE html>
<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:
```php
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'
]);
}
}
```
# Copyright
Mohammed Cherif BOUCHELAGHEM 2021
没有合适的资源?快使用搜索试试~ 我知道了~
适用于InertiaJS的PSR-15适配器_PHP_源码_下载.zip
共21个文件
php:16个
md:1个
json:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 18 浏览量
2023-09-17
16:22:02
上传
评论
收藏 26KB ZIP 举报
温馨提示
适用于InertiaJS的PSR-15适配器_PHP_源码_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
适用于InertiaJS的PSR-15适配器_PHP_源码_下载.zip (21个子文件)
inertia-psr15-master
src
ConfigProvider.php 1KB
Twig
InertiaExtension.php 475B
Middleware
InertiaMiddlewareFactory.php 614B
InertiaMiddleware.php 3KB
Model
Page.php 2KB
Exception
MissingInertiaConfigExeception.php 314B
Service
InertiaFactoryInterface.php 315B
InertiaFactory.php 1KB
RootViewProviderDecorator.php 658B
RootViewProviderInterface.php 199B
Inertia.php 3KB
InertiaInterface.php 367B
Factory
InertiaFactoryFactory.php 732B
RootViewProviderFactory.php 496B
LICENSE 1KB
composer.json 1KB
composer.lock 99KB
test
Middleware
InertiaMiddlewareTest.php 6KB
Service
InertiaTest.php 6KB
.gitignore 705B
README.md 5KB
共 21 条
- 1
资源评论
m0_57781768
- 粉丝: 9240
- 资源: 403
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功