Skip to content

Commit 73da6da

Browse files
committed
feat: Integrated external API and created virtual page
- Implemented API integration
1 parent 6e75e94 commit 73da6da

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

site/config/routes.php

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
<?php
22

3+
use Kirby\Cms\page;
4+
35
return [
4-
// your routes
6+
[
7+
'pattern' => '/articles.json',
8+
'method' => 'get',
9+
'action' => function () {
10+
$articles = site()
11+
->children()
12+
->listed()
13+
->filter('template', 'article')
14+
->flip();
15+
16+
return
17+
$articles->map(fn(Page $article) => [
18+
'title' => $article->title()->value(),
19+
'subtitle' => $article->subtitle()->value(),
20+
])->data();
21+
}
22+
],
23+
[
24+
'pattern' => '/first',
25+
'method' => 'get',
26+
'action' => function () {
27+
return Page::factory([
28+
'slug' => 'first',
29+
'template' => 'article',
30+
'content' => [
31+
'title' => 'Andrea\'s first virtual page',
32+
'subtitle' => 'Andrea\'s first virtual page',
33+
'published_at' => '2025-03-11',
34+
]
35+
]);
36+
}
37+
],
38+
[
39+
'pattern' => '/andrea/url/(:any)',
40+
'method' => 'get',
41+
'action' => function () {
42+
$mock = Remote::get('https://jsonplaceholder.typicode.com/posts/1');
43+
44+
$content = (object) json_decode($mock->content(), true);
45+
return Page::factory([
46+
'slug' => 'first',
47+
'template' => 'article',
48+
'content' => [
49+
'title' => $content->title,
50+
'subtitle' => $content->title,
51+
'published_at' => '2025-03-11',
52+
'blocks' => $content->body,
53+
]
54+
]);
55+
}
56+
],
57+
[
58+
'pattern' => '/search',
59+
'method' => 'get',
60+
'action' => function () {
61+
return Page::factory([
62+
'slug' => 'buscador',
63+
'template' => 'search',
64+
'content' => []
65+
]);
66+
}
67+
]
568
];

site/controllers/article.json.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
use Kirby\Cms\App;
3+
4+
return function (App $kirby, $site, $page) {
5+
$data = [
6+
'title' => $page->title()->value(),
7+
'subtitle' => $page->subtitle()->value(),
8+
];
9+
10+
return [
11+
'data' => $data,
12+
];
13+
};

site/templates/article.json.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
echo json_encode($data);

0 commit comments

Comments
 (0)