PHP code example of phpsoftbox / inertia

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

    

phpsoftbox / inertia example snippets


$response = $inertia->render('Home', [
    'title' => 'Inertia App',
]);

  new ServicesResetter($container, [
      Inertia::class => 'reset',
  ]);
  

// При сборке приложения (container factory):
$inertia->shareProvider(fn (): array => ['locale' => $translator->locale()], persistent: true, key: 'locale');

// В action / middleware после InertiaShareMiddleware:
$inertia->share('auth', ['user' => $user]);

// shared: ['menu' => ['items' => ['a', 'b', 'c']], 'app' => ['name' => 'Demo', 'area' => 'web']]
$inertia->render('Page', ['menu' => ['items' => ['x']], 'app' => ['area' => 'admin']]);
// props: ['menu' => ['items' => ['x']], 'app' => ['name' => 'Demo', 'area' => 'admin']]

use PhpSoftBox\Inertia\View\PhpViewRenderer;

$renderer = PhpViewRenderer::fromConfig($inertiaConfig, ['vite' => $vite]);

 use PhpSoftBox\Inertia\View\InertiaHtml; 

use PhpSoftBox\Inertia\Integration\ResourcePayloadNormalizer;

$normalizer = new ResourcePayloadNormalizer($resourceSerializer);

$inertia = new Inertia(
    // ...
    payloadNormalizer: $normalizer,
);

use PhpSoftBox\Inertia\Page\PageMeta;

$meta->setTitle('Панель управления')
    ->setDescription('Админ‑панель проекта')
    ->setKeywords(['admin', 'dashboard']);

return $inertia->render('Dashboard');

use PhpSoftBox\Inertia\Page\Breadcrumbs;

$breadcrumbs
    ->add('Главная', '/')
    ->add('Пользователи', '/users')
    ->add('Профиль', null, true);

return $inertia->render('Users/Show');

use PhpSoftBox\Inertia\Page\Menu;
use PhpSoftBox\Inertia\Page\MenuItem;
use PhpSoftBox\Inertia\Page\MenuMatchMode;

$menu = new Menu();
$menu->add(
    (new MenuItem('Dashboard', '/', 'dashboard', 'dashboard'))
        ->withMatchMode(MenuMatchMode::EQUALS),
);

$menu->add(
    (new MenuItem('Промо', null, 'promo', 'promo'))
        ->setChildren([
            new MenuItem('Промокоды', '/promo/promocodes', 'promocode', 'promocode'),
        ]),
);

use PhpSoftBox\Inertia\Page\Tabs;

$tabs
    ->setActiveKey('profile')
    ->add('Профиль', 'profile', '/profile')
    ->add('Пароль', 'password', '/profile/password');

return $inertia->render('Profile/Show');

use PhpSoftBox\Inertia\Ssr\HttpSsrRenderer;

$renderer = new HttpSsrRenderer(
    url: 'http://node:13714/render',
    timeout: 2.0,
);

use PhpSoftBox\Inertia\Area\InertiaAreaConfig;

return [
    'ssr' => env('INERTIA_SSR', false),

    'areas' => [
        'default' => 'web',

        'web' => new InertiaAreaConfig(
            pathPrefixes: ['/'],
            ssr: true,
            shared: [
                'app' => [
                    'area' => 'web',
                ],
            ],
        ),

        'admin' => new InertiaAreaConfig(
            pathPrefixes: ['/admin'],
            ssr: false,
            shared: [
                'app' => [
                    'area' => 'admin',
                ],
            ],
        ),
    ],
];

use PhpSoftBox\Inertia\Area\InertiaAreaConfig;

return [
    'areas' => [
        'default' => 'web',

        'web' => new InertiaAreaConfig(
            hosts: ['www.example.com', 'docs.example.com'],
            shared: [
                'app' => [
                    'area' => 'web',
                ],
            ],
        ),

        'admin' => new InertiaAreaConfig(
            hosts: ['admin.example.com'],
            ssr: false,
            shared: [
                'app' => [
                    'area' => 'admin',
                ],
            ],
        ),
    ],
];

use PhpSoftBox\Auth\Middleware\AreaAccessDeniedMode;
use PhpSoftBox\Auth\Middleware\AreaAccessRule;

new AreaAccessRule(
    area: 'admin',
    permission: 'admin.access',
    deniedMode: AreaAccessDeniedMode::NotFound,
)

use PhpSoftBox\Inertia\Area\AreaSharedDataProviderInterface;

final class AdminSharedDataProvider implements AreaSharedDataProviderInterface
{
    public function area(): string
    {
        return 'admin';
    }

    public function share(ServerRequestInterface $request): array
    {
        return [
            'admin' => [
                'sidebar' => [],
            ],
        ];
    }
}

'admin' => new InertiaAreaConfig(
    pathPrefixes: ['/admin'],
    ssr: false,
    shared: [
        'app' => [
            'area' => 'admin',
            'name' => 'Admin Panel',
        ],
    ],
),

final class AdminSharedDataProvider implements AreaSharedDataProviderInterface
{
    public function area(): string
    {
        return 'admin';
    }

    public function share(ServerRequestInterface $request): array
    {
        return [
            'app' => [
                'theme' => 'compact',
            ],
            'admin' => [
                'sidebar' => [],
            ],
        ];
    }
}

[
    'app' => [
        'area' => 'admin',
        'name' => 'Admin Panel',
        'theme' => 'compact',
    ],
    'admin' => [
        'sidebar' => [],
    ],
]

return [
    'app' => [
        'name' => 'Runtime Admin',
    ],
];