From 221e75824db44fa1c0b375c7f725552063c42d44 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 30 Aug 2019 19:59:08 +0300 Subject: [PATCH] feat(site-plugin): add ability to return entries in JSON Format #186 by adding param ?format=json --- site/plugins/site/app/Controllers/SiteController.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/site/plugins/site/app/Controllers/SiteController.php b/site/plugins/site/app/Controllers/SiteController.php index c212087a..506a3982 100644 --- a/site/plugins/site/app/Controllers/SiteController.php +++ b/site/plugins/site/app/Controllers/SiteController.php @@ -40,6 +40,9 @@ class SiteController extends Controller // Get uri $uri = $args['uri']; + // Is JSON Format + $is_json = ($query['format'] && $query['format'] == 'json') ? true : false; + // If uri is empty then it is main page else use entry uri if ($uri === '/') { $entry_uri = $this->registry->get('settings.entries.main'); @@ -81,6 +84,15 @@ class SiteController extends Controller // Run event onSiteEntryAfterInitialized $this->emitter->emit('onSiteEntryAfterInitialized'); + // Return in JSON Format + if ($is_json) { + if ($is_entry_not_found) { + return $response->withJson($this->entry, 404); + } + + return $response->withJson($this->entry); + } + // Set template path for current entry $path = 'themes/' . $this->registry->get('settings.theme') . '/' . (empty($this->entry['template']) ? 'templates/default' : 'templates/' . $this->entry['template']) . '.html';