1. Go to this page and download the library: Download leon-wbr/inertia-typo3 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class InertiaMiddleware extends \LeonWbr\Inertia\Middleware\InertiaMiddleware
{
public function share(ServerRequestInterface $request): array
{
return [
// Closures run when a page is rendered, with the routed request.
'navigation' => fn(ServerRequestInterface $request): array => $this->navigation->build($request),
];
}
}
#[AsEventListener]
final class ProductPageListener
{
public function __construct(private readonly InertiaService $inertia) {}
public function __invoke(BeforeInertiaPageRenderedEvent $event): void
{
if ((int)$event->getPage()['layout'] !== 3) {
return;
}
$event->setComponent('Products');
$event->addProp('products', $this->inertia->merge(fn() => $this->products->page(1)));
$event->addProp('stats', $this->inertia->defer(fn() => $this->products->stats()));
$event->addProp('filters', $this->inertia->optional(fn() => $this->products->filters()));
}
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$name = trim((string)($request->getParsedBody()['name'] ?? ''));
if ($name === '') {
$this->inertia->withErrors(['name' => 'Please enter your name.']);
} else {
$this->inertia->flash('success', 'Thanks!');
}
return $this->inertia->redirect($request->getUri()->getPath()); // 303 See Other
}