Hello!
I’m developing a website with a React frontend for the first time. I want to use Kirby in headless mode to manage my content, which I’ll fetch via API.
The thing is, I’m not sure if how I plan to do it is a good idea.
Is it good practice to set up Kirby like this if it’s read-only?
If there is a more convenient or just better way to do this let me know.
I just want to keep my app as simple as possible, since I’m not experienced with react … ![]()
So, I’m planning to set up Kirby like this:
<?php
# config.php
return [
'cors' => [
'allowOrigin' => 'https://UrlToMyReactFrontend.com',
# maybe more options needed …
]
];
<?php Kirby::plugin('xx/custom-api', [
'routes' => [
[
'pattern' => 'public-api/(:all)',
'method' => 'GET',
'action' => function ($slug) {
$page = page($slug);
if (!$page) return Response::json(['status' => 'error', 'message' => 'Page not found'], 404);
# more code
# more code
# more code
# return stuff here
return Response::json([
'title' => $page->title()->value(),
]);
}
]
]
]);
?>
my react app fetches data like this:
const API_BASE = 'https://UrlToMyKirbyBackend.com/public-api';
export async function getPage(slug) {
const res = await fetch(`${API_BASE}/${slug}`);
if (!res.ok) {
throw new Error(`Kirby API error: ${res.status}`);
}
return res.json();
}
thank you already!