<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mec Rawlings on Medium]]></title>
        <description><![CDATA[Stories by Mec Rawlings on Medium]]></description>
        <link>https://medium.com/@mecrawlings?source=rss-11eb5709c3f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Ar2igTUsHkmypfT8xqA13Q.png</url>
            <title>Stories by Mec Rawlings on Medium</title>
            <link>https://medium.com/@mecrawlings?source=rss-11eb5709c3f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 18:31:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mecrawlings/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Decoupled Kirby]]></title>
            <link>https://medium.com/@mecrawlings/decoupled-kirby-d22416f567e9?source=rss-11eb5709c3f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/d22416f567e9</guid>
            <category><![CDATA[headless-cms]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[simple-tooling]]></category>
            <category><![CDATA[kirby-cms]]></category>
            <category><![CDATA[decoupled-cms]]></category>
            <dc:creator><![CDATA[Mec Rawlings]]></dc:creator>
            <pubDate>Tue, 23 May 2017 14:40:13 GMT</pubDate>
            <atom:updated>2017-05-23T14:51:03.606Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGqcSdS5kRvddPuoHf-5ZQ.jpeg" /><figcaption>Not that Kirby!</figcaption></figure><p>It’s becoming super popular to ‘decouple’ your CMS from your frontend implementation. The approach brings a lot of benefits: we can access our content on more platforms, we can replace the backend and re-expose the same endpoints so our frontend still works, we can scale etc.</p><p>I wanted to use my favourite CMS, <a href="https://getkirby.com/">Kirby</a>, to provide content whilst I am trying to learn some <a href="https://facebook.github.io/react/">React</a>.</p><p>I love Kirby (no, they don’t pay me to say that!), everything I want, it just seems to already do or it’s super easy to get it done yourself. To quote Kirby itself:</p><blockquote>…easy to setup, easy to use, flexible as hell</blockquote><p>Getting my Kirby api up and running was so ridiculously easy, I thought I’d share the process.</p><h3>The goal</h3><p>So the goal is to download the Kirby starter kit, which is Kirby itself, a simple theme and some content. We then want to expose this content via a RESTful API so we can consume it from a React (or something) client app and preferable still use the site and panel to add content. In my case this client app will be a simple, single page app site.</p><p>We’re not building the client here only the api we can use to get the content from Kirby and the api will be read only via GET requests.</p><h3>Get Kirby</h3><p>Download the Kirby Starter Kit and unzip it:</p><p><a href="https://getkirby.com/downloads">Downloads | Kirby</a></p><p>I’ll assume you have a PHP environment you can run Kirby on, I just use the server built in to PHP, but MAMP or WAMP (or similar) would work equally well, anything that meets the <a href="https://getkirby.com/docs/installation/download">Kirby system requirements</a>.</p><p>Move into the Kirby folder and start your server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iEyVCC8FAqGT-uRw4GVLwQ.png" /><figcaption>Kirby Starter Kit</figcaption></figure><p>That’s our Kirby starterkit up and running! Have a nose around to familiarise yourself with the content.</p><p>Now let’s think about which content we want the api to expose to get a simple single page site working:</p><ul><li>The home page of the site</li><li>The top level pages for navigation</li><li>A particular page (projects, about, contact, etc)</li><li>A particular child page of a page (projects &gt; project-a, etc)</li><li>All of the child pages of a page (project-a, project-b, project-d, etc)</li></ul><p>We should also return an error if the page doesn’t exist.</p><p>We need to add the routes for the endpoints we want to use in our api. Kirby has two ways to add the routes we need. One is to <a href="https://getkirby.com/docs/developer-guide/advanced/routing">add the routes to the Kirby config</a> which is a bit site specific for us.</p><p>The other is to create a plugin. Creating a plugin let’s us wrap our code in a nice manageable bundle and it’s super easy with Kirby.</p><h3>Create the plugin</h3><p>In the Kirby folder, navigate to ‘site &gt; plugins’ and create a folder there called <em>kirby-simple-api </em>inside that folder, create a file called <em>kirby-simple-api.php.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*jvPxe6ShUR876H3XAAsOTQ.png" /><figcaption>Kirby plugin structure</figcaption></figure><p>Both the folder and the file need to have the same name, it doesn’t matter what the name is as long as they are the same.</p><p>That’s it! Our plugin is setup and running, it’s just not doing anything right now – let’s add our routes.</p><h3>Adding routes</h3><p>Our routes go into our plugin file. The routes are pretty straight forward, we register them at a url and send the content back. If we can’t locate the page with the supplied name, we error.</p><p>The trick for me was the slightly different syntax of the routes in the plugin as opposed to the Kirby config file, which I’d used before. I had to create each route separately rather than one big array of routes as you do in the config file.</p><p>The rest is standard Kirby stuff, get the site, get the pages we want and return them, Kirby even converts them to json for us (thank you Kirby!).</p><p>Here’s my code for the <em>kirby-simple-api.php</em> file:</p><pre>&lt;?php</pre><pre>kirby()-&gt;set(&#39;route&#39;, array(<br>        &#39;pattern&#39; =&gt; &#39;api/menu&#39;,<br>        &#39;action&#39;  =&gt; function() {<br>            return response::json(site()-&gt;pages()-&gt;visible()-&gt;toJson());<br>        }<br>  ));</pre><pre>kirby()-&gt;set(&#39;route&#39;, array(<br>        &#39;pattern&#39; =&gt; &#39;api/home&#39;,<br>        &#39;action&#39;  =&gt; function() {<br>            if ( site()-&gt;homePage() ) {<br>              return response::json(site()-&gt;homePage()-&gt;toJson());<br>            } else {<br>              return response::error($message = &#39;404 Not found&#39;, $code = 404, $data = array($section));<br>            }<br>        }<br>  ));</pre><pre>kirby()-&gt;set(&#39;route&#39;, array(<br>        &#39;pattern&#39; =&gt; &#39;api/(:any)&#39;,<br>        &#39;action&#39;  =&gt; function($page) {<br>            if ( site()-&gt;pages()-&gt;find($page) ) {<br>              return response::json(site()-&gt;page($page)-&gt;toJson());<br>            } else {<br>              return response::error($message = &#39;404 Not found&#39;, $code = 404, $data = array($page));<br>            }<br>        }<br>  ));</pre><pre>kirby()-&gt;set(&#39;route&#39;, array(<br>        &#39;pattern&#39; =&gt; &#39;api/(:any)/children&#39;,<br>        &#39;action&#39;  =&gt; function($page) {<br>            if ( site()-&gt;pages($page) &amp;&amp; site()-&gt;pages($page)-&gt;children()-&gt;visible() ) {<br>              return response::json(site()-&gt;page($page)-&gt;children()-&gt;visible()-&gt;toJson());<br>            } else {<br>              return response::error($message = &#39;404 Not found&#39;, $code = 404, $data = array($page));<br>            }<br>        }<br>  ));</pre><pre>kirby()-&gt;set(&#39;route&#39;, array(<br>        &#39;pattern&#39; =&gt; &#39;api/(:any)/(:any)&#39;,<br>        &#39;action&#39;  =&gt; function($section, $page) {<br>            if ( site()-&gt;page($section)-&gt;children()-&gt;find($page) ) {<br>              return response::json(site()-&gt;page($section)-&gt;children()-&gt;find($page)-&gt;toJson());<br>            } else {<br>              return response::error($message = &#39;404 Not found&#39;, $code = 404, $data = array($section, $page));<br>            }<br>        }<br>  ));</pre><p>You can view the source on <a href="https://github.com/mec/kirby-simple-api/blob/master/kirby-simple-api/kirby-simple-api.php">GitHub</a> which is easier on the eyes!</p><p>Once the plugin file is saved we should be able to use the endpoints and get data back.</p><h4>Testing</h4><p>Use a browser or REST client to check your endpoints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EqkeWSu6HZ_5td98Mh21mg.png" /><figcaption>The Kirby Startekit home page via the api</figcaption></figure><p>We’ve built the five endpoints we set out in our goals.</p><p>/api/menu</p><p>/api/home</p><p>/api/&lt;page&gt;</p><p>/api/&lt;page&gt;/children</p><p>/api/&lt;page&gt;/&lt;child&gt;</p><p>For dynamic endpoints and the Kirby starterkit content we can use endpoints like : /api/projects/project-a, /api/projects/project-e, /api/about, etc.</p><p>And to throw and error supply a unknown endpoint : /api/unknownpage.</p><p>This complete plugin is on <a href="https://github.com/mec/kirby-simple-api">GitHub</a>.</p><h4>Conclusion</h4><p>I hope this shows how easy it is to work with Kirby and do some pretty cool stuff with little effort and I hope it encourages you to try Kirby out (it’s free to develop locally) if you haven’t already.</p><p>One of the many things I like about this setup is that I can login to the Kirby panel and add new top level pages with children and access them via the api with no extra effort, which is pretty handy! If I add a Work page with children, I can access the content at: /api/work, api/work/work-a, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f63rgIYiAN98SpJ8KS_egw.png" /><figcaption>Kirby panel with new ‘Work’ content</figcaption></figure><p>There’s obviously a lot to do to this, it’s not meant for production but it’s a nice, simple and fast way to work with json data straight out of Kirby.</p><p>I was just blown away by how easy this was to get up and running – I can focus on the hard part (learning React!) now!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d22416f567e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>