1. Go to this page and download the library: Download karewan/knroute-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/ */
use Karewan\KnRoute\Attributes\Get;
use Karewan\KnRouteInertia\Inertia;
final class EventsController
{
#[Get('/events/{id:uint}')]
public function show(int $id): void
{
$event = EventsDao::get($id) ?? throw new HttpException(404);
Inertia::render('Events/Show', [
'event' => ['id' => $event->id, 'title' => $event->title],
]);
}
}
Inertia::render('Dashboard', [
'auth' => fn() => [
'user' => currentUser(),
'notifications' => Inertia::defer(fn() => NotificationsDao::unread()),
],
// Nested into "auth" when the page is resolved
'auth.permissions' => fn() => currentUser()->permissions(),
]);
final class Avatar implements ProvidesInertiaProperty
{
public function __construct(private readonly User $user) {}
public function toInertiaProperty(PropertyContext $context): mixed
{
return $this->user->avatarUrl();
}
}
Inertia::merge($items); // append at the root
Inertia::merge($items)->prepend(); // prepend at the root
Inertia::merge($page)->append('data', matchOn: 'id'); // append to a nested array, update the items with the same id
Inertia::merge($forum)->append('posts')->prepend('announcements');
Inertia::merge($data)->append(['users.data' => 'id', 'messages']);
Inertia::deepMerge($chat)->matchOn('messages.id');
Inertia::defer(fn() => $results)->deepMerge();
'plans' => Inertia::once(fn() => PlansDao::all()),
'rates' => Inertia::once(fn() => RatesDao::all())->until(3600), // seconds, DateInterval or DateTimeInterface
'roles' => Inertia::once(fn() => RolesDao::all())->as('roles'), // shared by the pages using the same key
'prices' => Inertia::once(fn() => PricesDao::all())->fresh($changed), // force a new value
'stats' => Inertia::defer(fn() => StatsDao::compute())->once(), // also on optional and merge props
Inertia::redirect('/users'); // 302, or 303 after PUT, PATCH and DELETE
Inertia::back(); // the Referer of this host, or "/"
Inertia::back('/users'); // with a fallback
Inertia::location('https://example.com/logout'); // full page visit, 409 + X-Inertia-Location for Inertia visits
Inertia::preserveFragment(); // keep the fragment of the visited URL across the redirect
Inertia::encryptHistory(); // this page
Inertia::encryptHistory(false); // not this page, whatever the configuration
Inertia::clearHistory(); // rotate the key, e.g. on logout: the encrypted history becomes unreadable
use Karewan\KnRouteInertia\Ssr\HttpSsrGateway;
Inertia::configure(
rootView: __DIR__ . '/App/Views/app.php',
ssr: new HttpSsrGateway('http://127.0.0.1:13714/render', timeout: 2.0),
);
Inertia::configure(
rootView: $rootView,
session: new NativeSessionStore(static fn() => AppSession::start()),
);
public function before(): void
{
$token = $_SESSION['csrf'] ??= bin2hex(random_bytes(32));
setcookie('XSRF-TOKEN', $token, ['path' => '/', 'samesite' => 'Lax', 'secure' => true]);
if (!in_array(HttpUtils::getMethod(), ['GET', 'HEAD', 'OPTIONS'], true)
&& !hash_equals($token, HttpUtils::getHeader('X-XSRF-TOKEN'))) {
throw new HttpException(419, 'The page expired, please try again.');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.