You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Kirby Query Language (KQL)](#kirby-query-language-kql)
32
+
-[Templates](#templates)
33
+
-[Panel Settings](#panel-settings)
34
+
-[Field Methods](#field-methods)
35
+
-[`toResolvedBlocks()`](#toresolvedblocks)
36
+
-[`resolvePermalinks()`](#resolvepermalinks)
37
+
-[Page Methods](#page-methods)
38
+
-[`i18nMeta()`](#i18nmeta)
39
+
-[API Builder](#api-builder)
40
+
-[FAQ](#faq)
41
+
22
42
## Use Cases
23
43
24
44
This plugin is designed for developers who want to leverage Kirby's backend to serve content to a frontend application, static site generator, or mobile app. You can either opt-in to headless functionality for your existing Kirby site or use this plugin to build a headless-first CMS from scratch.
25
45
26
46
Here are scenarios where the Kirby Headless plugin is particularly useful:
27
47
28
48
- 1️⃣ If you prefer to query data using the [Kirby Query Language](#kirby-query-language-kql).
29
-
- 2️⃣ If you want to use [Kirby's default template system](#templates) to output JSON.
49
+
- 2️⃣ If you want to use [Kirby's default template system](#templates) to output JSON instead of HTML.
30
50
31
51
Detailed instructions on how to use these features can be found in the [usage](#usage) section.
32
52
@@ -75,6 +95,43 @@ This will make all page templates return JSON instead of HTML by [defining globa
75
95
76
96
## Usage
77
97
98
+
### Private vs. Public API
99
+
100
+
It is recommended to secure your API with a token. To do so, set the `headless.token` Kirby configuration option:
101
+
102
+
```php
103
+
# /site/config/config.php
104
+
return [
105
+
'headless' => [
106
+
'token' => 'test'
107
+
]
108
+
];
109
+
```
110
+
111
+
You will then have to provide the HTTP header `Authentication: Bearer ${token}` with each request.
112
+
113
+
> [!WARNING]
114
+
> Without a token your page content will be publicly accessible to everyone.
115
+
116
+
### Cross Origin Resource Sharing (CORS)
117
+
118
+
CORS is enabled by default. You can enhance the default CORS configuration by setting the following options in your `config.php`:
It is common to authenticate API requests with a token, which is not possible with the default KQL endpoint. Thus, this plugin adds a new KQL endpoint under `/api/kql` that supports bearer token authentication and query response caching.
@@ -143,43 +200,6 @@ To **disable** the bearer token authentication for your Kirby instance and inste
143
200
> [!NOTE]
144
201
> The KQL default endpoint `/api/query` remains using basic authentication and also infers the `kql.auth` config option.
145
202
146
-
### Private vs. Public API
147
-
148
-
It is recommended to secure your API with a token. To do so, set the `headless.token` Kirby configuration option:
149
-
150
-
```php
151
-
# /site/config/config.php
152
-
return [
153
-
'headless' => [
154
-
'token' => 'test'
155
-
]
156
-
];
157
-
```
158
-
159
-
You will then have to provide the HTTP header `Authentication: Bearer ${token}` with each request.
160
-
161
-
> [!WARNING]
162
-
> Without a token your page content will be publicly accessible to everyone.
163
-
164
-
### Cross Origin Resource Sharing (CORS)
165
-
166
-
CORS is enabled by default. You can enhance the default CORS configuration by setting the following options in your `config.php`:
Write templates as you would in any other Kirby project. But instead of returning HTML, they return JSON. The internal route handler adds the correct content type and also handles caching (if enabled).
@@ -367,13 +387,13 @@ return [
367
387
368
388
#### Mutate Blocks Values
369
389
370
-
By default, resolved fields don't mutate source fields of the blocks array. Instead, the resolved content is stored in the `resolved` key of each block. If you want to overwrite the field value with the resolved content, you can set the `overwriteContent` option to `true`:
390
+
By default, resolved fields don't mutate source fields of the blocks array. Instead, the resolved content is stored in the `resolved` key of each block. If you want to overwrite the field value with the resolved content, you can set the `replaceValues` option to `true`:
371
391
372
392
```php
373
393
# /site/config/config.php
374
394
return [
375
395
'blocksResolver' => [
376
-
'overwriteContent' => true
396
+
'replaceValues' => true
377
397
]
378
398
];
379
399
```
@@ -426,9 +446,19 @@ return [
426
446
427
447
The `i18nMeta()` method returns an array including the title and URI for the current page in all available languages. This is useful for the frontend to build a language switcher.
428
448
429
-
## Advanced
449
+
**Type Declaration:**
450
+
451
+
```ts
452
+
typeI18nMeta=Record<
453
+
string,
454
+
{
455
+
title:string
456
+
uri:string
457
+
}
458
+
>
459
+
```
430
460
431
-
###API Builder
461
+
## API Builder
432
462
433
463
This headless starter includes an Express-esque API builder, defined in the [`JohannSchopplich\Headless\Api\Api` class](./src/classes/Api/Api.php). You can use it to re-use logic like handling a token or verifying some other incoming data.
0 commit comments