PHP code example of leon-wbr / inertia-typo3

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/ */

    

leon-wbr / inertia-typo3 example snippets


return [
    'frontend' => [
        'leon-wbr/inertia' => [
            'target' => \MyVendor\MySitepackage\Middleware\InertiaMiddleware::class,
        ],
    ],
];

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
}

$event->addProp('plans', $this->inertia->once(fn() => $this->plans->all()));
$event->addProp('countries', $this->inertia->once(fn() => $this->countries())->as('countries-v2')->until(3600));
$event->addProp('stats', $this->inertia->defer(fn() => $this->stats())->once());

  public function shareOnce(ServerRequestInterface $request): array
  {
      return ['labels' => fn() => $this->labels->forLanguage($request)];
  }
  

use TYPO3\CMS\Core\Pagination\ArrayPaginator;

$page = max(1, (int)($request->getQueryParams()['page'] ?? 1));

// From a TYPO3 paginator (ArrayPaginator, QueryBuilderPaginator, Extbase's
// QueryResultPaginator, …): sent as {data: [...], meta: {currentPage, lastPage}}.
$event->addProp('articles', $this->inertia->scroll(
    fn() => new ArrayPaginator($articles, $page, 10),
));

// From a list and the metadata as an array: sent as {data: [...]}.
$event->addProp('posts', $this->inertia->scroll($posts, metadata: [
    'currentPage' => $page,
    'previousPage' => $page > 1 ? $page - 1 : null,
    'nextPage' => $hasMore ? $page + 1 : null,
]));

use LeonWbr\Inertia\Testing\AssertableInertia;
use LeonWbr\Inertia\Testing\InteractsWithInertia;

final class ProductsTest extends FunctionalTestCase
{
    use InteractsWithInertia;

    #[Test]
    public function listsProducts(): void
    {
        $response = $this->executeFrontendSubRequest($this->inertiaRequest('https://website.local/products'));

        $this->assertInertia($response, fn(AssertableInertia $page) => $page
            ->component('Products')
            ->has('products.data', 10)
            ->where('filters.q', '')
            ->where('products.data.0.title', fn(string $title): bool => $title !== '')
            ->missing('internal')
            ->hasDeferredProps('default', ['stats'])
            ->hasMergeProps(['products.data'])
            ->hasOnceProp('categories')
            ->hasScrollProp('products', ['nextPage' => 2])
            ->hasNoErrors()
            ->version(''));
    }
}