I think it’s the other way around, calling the /home/api URL (so the page data will be from content/home/api/homeapi.txt) and trying to show data from the home page (content/home/home.txt).
// Pattern: ([a-z0-9\/]).json
function($uri) {
$page = page($uri);
if (!$page) return false;
// or make a Response object using fields from $page,
// converted to JSON
};
For instance with this you can return the raw field data for any page in the site as a JSON response:
c::set('routes', [
// Return a page's full content as JSON, for any page.
// (Would need a bit more work for a multilingual site.)
[
'pattern' => '([a-z0-9\\/]+).json',
'action' => function($uri) {
$page = page($uri);
if (!$page) {
$content = '{}';
if ($errorPage = page(c::get('error', 'error'))) {
$content = ['title' => $errorPage->title()->value];
}
return new Response($content, 'json', 404);
}
return new Response($page->content()->toArray(), 'json', 200);
}
]
]);
Calling /home.json, /some/other/page.json etc.
Of course you will probably want to be more specific. Eg. only returning a JSON response for some pages or only returning some of the fields (perhaps even with some transformations, like returning HTML content instead of Markdown/Kirbytext).