PHP code example of nytodev / inertia-bundle

1. Go to this page and download the library: Download nytodev/inertia-bundle 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/ */

    

nytodev / inertia-bundle example snippets


Nytodev\InertiaBundle\InertiaBundle::class => ['all' => true],

use Nytodev\InertiaBundle\Service\Inertia;

class UserController
{
    #[Route('/users')]
    public function index(Inertia $inertia): Response
    {
        return $inertia->render('Users/Index', [
            'users' => $this->repository->findAll(),
        ]);
    }

    #[Route('/users/{id}')]
    public function show(User $user, Inertia $inertia): Response
    {
        return $inertia->render('Users/Show', [
            'user' => $user,
        ]);
    }
}

// src/EventListener/InertiaShareListener.php
class InertiaShareListener
{
    public function __construct(
        private readonly Inertia $inertia,
        private readonly Security $security,
    ) {}

    public function onKernelRequest(RequestEvent $event): void
    {
        $this->inertia->share('auth', [
            'user' => $this->security->getUser()?->getUserIdentifier(),
        ]);
    }
}

return $inertia->render('Feed/Index', [
    'posts'    => $inertia->merge(fn () => $this->postRepo->paginate($page)),
    'stats'    => $inertia->defer(fn () => $this->buildStats()),
    'comments' => $inertia->lazy(fn () => $this->commentRepo->findAll()),
]);
jsx
// assets/Pages/Users/Index.jsx
export default function UsersIndex({ users }) {
    return (
        <ul>
            {users.map(user => (
                <li key={user.id}>{user.name}</li>
            ))}
        </ul>
    )
}