All Projects → jongacnik → kirby2-sirvy

jongacnik / kirby2-sirvy

Licence: MIT license
🔗 Kirby Services API

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to kirby2-sirvy

Kirby Git Content
Commit and Push changes made via the Panel
Stars: ✭ 92 (+64.29%)
Mutual labels:  kirby
Kirby Webpack
💪 A Kirby CMS starter-kit with modern frontend tools
Stars: ✭ 150 (+167.86%)
Mutual labels:  kirby
laravel-mix-kirby
Laravel Mix helper for Kirby
Stars: ✭ 23 (-58.93%)
Mutual labels:  kirby
Kirby Matomo
Matomo integration for Kirby, in both your panel and templates. Kirby 3 only.
Stars: ✭ 103 (+83.93%)
Mutual labels:  kirby
Kirby Secrets
Unofficial documentation for Kirby CMS. It's is NOT maintained by the Kirby crew.
Stars: ✭ 136 (+142.86%)
Mutual labels:  kirby
Kirby Visual Markdown
Visual Markdown Editor for Kirby CMS 2
Stars: ✭ 171 (+205.36%)
Mutual labels:  kirby
Kirby Annotator
Kirby field for adding notes to images by pinning them to specific coordinates. Kirby 2 and 3.
Stars: ✭ 84 (+50%)
Mutual labels:  kirby
kirby-git
Kirby plugin for updating content in the panel via Git
Stars: ✭ 75 (+33.93%)
Mutual labels:  kirby
Kirby Autogit
⬢ Saves every change made via Kirby Panel to a Git repository
Stars: ✭ 147 (+162.5%)
Mutual labels:  kirby
kirby-highlighter
🌐 Server-side syntax highlighting for the Kirby code block & KirbyText
Stars: ✭ 18 (-67.86%)
Mutual labels:  kirby
Shopkit
Comprehensive commerce solution for Kirby CMS v2
Stars: ✭ 103 (+83.93%)
Mutual labels:  kirby
Kirby Imageset
A flexible, responsive image component for Kirby 2, featuring lazy-loading, fancy placeholders and much more.
Stars: ✭ 122 (+117.86%)
Mutual labels:  kirby
Editor
A new type of WYSIWYG editor for Kirby
Stars: ✭ 209 (+273.21%)
Mutual labels:  kirby
Starterkit
Kirby's sample site – the easiest way to get started with Kirby
Stars: ✭ 102 (+82.14%)
Mutual labels:  kirby
kirby-highlight
Themeable server-side syntax highlighting for Kirby
Stars: ✭ 14 (-75%)
Mutual labels:  kirby
Field Multiselect
Multiselect field for Kirby 2 CMS
Stars: ✭ 84 (+50%)
Mutual labels:  kirby
Kirby Focus
Better image cropping for Kirby CMS
Stars: ✭ 170 (+203.57%)
Mutual labels:  kirby
kirby-hashed-assets
🛷 File name hashes support for css() and js() helpers. Without rewrite rules!
Stars: ✭ 15 (-73.21%)
Mutual labels:  kirby
kirby-podcast
A KirbyCMS-Podcast-Plugin
Stars: ✭ 22 (-60.71%)
Mutual labels:  kirby
Kirby Uniform
A versatile Kirby plugin to handle web form actions.
Stars: ✭ 214 (+282.14%)
Mutual labels:  kirby

Sirvy: Kirby Services API

This is a plugin for Kirby that introduces an extensible URL based API for performing services such as resizing images or returning page contents as json.

Installation

Put the sirvy folder in /site/plugins.

What does this do?

Sirvy injects a /sirvy/(:all?) route into your site. This route passes data to PHP functions (we're calling them services).

For example, after you've added Sirvy to your project, you can access your homepage contents as json via this url:

http://yourkirby.com/sirvy/home?service=json

In fact, you could access any page's contents as json:

http://yourkirby.com/sirvy/some/uri/here?service=json

The /sirvy route grabs the page object based on the uri and passes it into a service function. In this case it is being passed into the json service function. The json service takes a page object and returns the response as json.

Services

Sirvy comes with 4 services built-in, but you can add as many services as you need!

tree

/sirvy/home?service=tree

returns page contents, files, and children recursively as json

json

/sirvy/home?service=json

returns page contents as json

resize

/sirvy/home?service=resize&image=image.jpg&width=400

returns a resized thumbnail

crop

/sirvy/home?service=crop&image=image.jpg&width=200&height=300

returns a cropped thumbnail

Sirvy Page Method

Instead of manually building up the URLs for services, a Sirvy page method is exposed to make this super simple:

page('home')->sirvy('json');
// returns
// -> http://yourkirby.com/sirvy/home?service=json

The first parameter is always the name of the service, the second optional parameter is for passing in an array of additional data:

page('home')->sirvy('crop', [
  'image'  => page('home')->image()->filename(),
  'width'  => 200,
  'height' => 300
]);
// returns
// -> .../sirvy/home?service=crop&image=image.jpg&width=200&height=300

This is great for spitting out many thumbnails without blocking initial page render:

<? foreach ($hundredsOfImages as $i) :
  $thumb = $page->sirvy('resize', [
    'image' => $i->filename(),
    'width' => 500
  ]) ?>
  <img src="<?= $thumb ?>">
<? endforeach ?>

Custom Services

The real power of Sirvy is in defining your own services. You want an API to expose not just the current page's content as json, but all of its children and files as well? You can make a service for that! You want an API to expose the page's contents as a PHP array? You can make a service for that!

Services are registered 2 ways:

  1. From PHP files in the services folder inside of the Sirvy plugin folder
  2. By registering them as an option in your config.php

The 3 included services (json, resize, and crop) are all defined as files in the services folder. The name of the file becomes the name of the service (json.php -> json). To add your own services, just add more files to this folder. The file needs to return a function. Unless you are returning media files, you will typically want this function to return a Kirby response object. Take a peek at the included services as examples.

You can also register services via an option in your config.php. The key of the array becomes the name of the service. Here is how you could add a toArray service that exposes the page's contents as a PHP array:

c::set('sirvy.services', [
  'toArray' => function ($page, $data) {
    return new Response(print_r($page->toArray()));
  }
]);

// you can now use
page('home')->sirvy('toArray')

// which returns
// -> http://yourkirby.com/sirvy/home?service=toArray

// That url would respond with the page object as a plain PHP array!

Caching

Caching is enabled on a per-service basis using the Kirby file cache. This is great if you want to cache partials like json or html snippets:

/sirvy/home?service=json&cache=1

This json response will be cached in the Kirby cache directory.

By default the cached items will never expire, but you can change this with an option. The cache is always flushed when making content changes via the panel.

Caching will only work when your service returns a Kirby response object. I also wouldn't recommend trying to cache a non-text response (like image or video). I haven't put in catches so weirdness will likely happen.

Options

// change the sirvy path
c::set('sirvy.path', 's');
// -> http://yourkirby.com/s/home?service=json

// expire cached responses after an hour
c::set('sirvy.cache.duration', 60);

// register additional services
c::set('sirvy.services', [
  'serviceName' => function ($page, $data) {
    return new Response('content');
  }
]);

// enable cors on sirvy requests
c::set('sirvy.cors', '*');

Errors

If Sirvy encounters an error, such as being unable to find a page uri or service, a json error response will be returned (status code 400).

Security

As brought up in the early 2.3 discussions, an API like this could potentially lead to misuse when considering image manipulation. I might look into adding some built-in options for rate limiting or auth, but in the meantime if this is a concern for you, nothing stopping you from adding the functionality into the services yourself!

Todo

  • Rate Limiting? See Security

Author

Jon Gacnik http://jongacnik.com

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].