-
Notifications
You must be signed in to change notification settings - Fork 1
/
kirby-api.php
56 lines (47 loc) · 1.74 KB
/
kirby-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/*
* Kirby Basic JSON REST API Plugin
* A plugin for Kirby - https://getkirby.com/
* Version: 0.0.3
*
* Author: Butch Ewing @butchewing
*/
// Feel free to set this to whatever you would like.
$prefix = "api/v1/";
kirby()->routes(array(
array(
'method' => 'GET',
'pattern' => $prefix . '(:all)',
'action' => function() use($prefix) {
$path = kirby()->request()->path();
$collection = str_replace($prefix, '', $path);
$api = array();
$api['page'] = page($collection)->toArray();
$api['images'] = page($collection)->images()->toArray();
// Commented out because is doesn't work in the current version of Kirby.
//$api['documents'] = page($collection)->documents()->toArray();
$documents = page($collection)->documents();
$docs = array();
foreach($documents as $doc) {
$docs[] = array(
'url' => $doc->url(),
'filename' => $doc->filename(),
'niceSize' => $doc->niceSize()
);
}
$api['documents'] = $docs;
// Commented out because it is not usually nessesary.
//$api['siblings'] = page($collection)->siblings()->toArray();
$children = page($collection)->children()->toArray();
$childrenImages = array();
foreach($children as $item) {
$item['images'] = page($item['id'])->images()->toArray();
$childrenImages[] = $item;
}
$api['children'] = $childrenImages;
// Commented out because it is not usually nessesary.
//$api['grandchildren'] = page($collection)->grandchildren()->toArray();
return new Response($api, 'json');
}
)
));