1. Go to this page and download the library: Download monoeq/kirby-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/ */
return function ($page, $site, $kirby) {
// Form Handling
if (kirby()->request()->method() === 'POST') {
$kirby->impersonate('kirby');
$page->changeTitle(get('title', ''));
go($page); // <- Redirect back to GET request for Inertia
}
return Inertia::render(
$page->intendedTemplate(),
$page->toArray()
);
};
return function ($page, $site, $kirby) {
return Inertia::render(
$page->intendedTemplate(),
$page->toArray(),
[ 'meta' => 'hello' ]
);
};
// In your template:
<?= $meta
return [
'monoeq.inertia.shared' => [
'messages' => function () {
// pull() fetches any session data stored under messages, and then wipes it
return InertiaSession::pull('messages');
},
'errors' => function () {
// pull() fetches any session data stored under errors, and then wipes it
return InertiaSession::pull('errors');
}
]
];
return function ($page, $site, $kirby) {
// Form Handling
if (kirby()->request()->method() === 'POST') {
try {
$kirby->impersonate('kirby');
$page->changeTitle(get('title', ''));
InertiaSession::append('messages', 'Thank You!');
} catch (Exception $e) {
InertiaSession::append('errors', $e->getMessage());
// or if you want to have named errors
// InertiaSession::merge('errors', [ 'title' => $e->getMessage() ]);
}
go($page); // <- Redirect back to GET request for Inertia
}
return Inertia::render(
$page->intendedTemplate(),
$page->toArray()
);
};