From 0e80beb950b6c75d59858d7118132516b77b1eb7 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione Date: Sat, 20 Apr 2019 19:27:31 +0200 Subject: [PATCH] Add the possibility to set HTTP response status in pages frontmatter --- formwork/Core/Page.php | 24 ++++++++++++++++++++++-- formwork/Core/Site.php | 2 -- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/formwork/Core/Page.php b/formwork/Core/Page.php index 528ef2222..e6d1f783a 100755 --- a/formwork/Core/Page.php +++ b/formwork/Core/Page.php @@ -6,6 +6,7 @@ use Formwork\Parsers\ParsedownExtension as Parsedown; use Formwork\Parsers\YAML; use Formwork\Utils\FileSystem; +use Formwork\Utils\Header; use Formwork\Utils\HTTPRequest; use Formwork\Utils\Uri; use RuntimeException; @@ -382,12 +383,16 @@ public function renderToString($vars = array()) /** * Render page and return rendered content * - * @param array $vars Variables to pass to the page + * @param array $vars Variables to pass to the page + * @param bool $sendHeaders Whether to send headers before rendering * * @return string */ - public function render($vars = array()) + public function render($vars = array(), $sendHeaders = true) { + if ($sendHeaders) { + $this->sendHeaders(); + } echo $renderedPage = $this->renderToString($vars); return $renderedPage; } @@ -488,6 +493,21 @@ protected function processData() if (is_null($this->num()) || $this->template()->scheme()->get('num') === 'date') { $this->sortable = false; } + + // Set default 404 Not Found status to error page + if ($this->isErrorPage() && !$this->has('response-status')) { + $this->set('response-status', 404); + } + } + + /** + * Send page headers + */ + protected function sendHeaders() + { + if ($this->has('response-status')) { + Header::status($this->get('response-status')); + } } public function __toString() diff --git a/formwork/Core/Site.php b/formwork/Core/Site.php index 312da0d4a..2ede52c92 100755 --- a/formwork/Core/Site.php +++ b/formwork/Core/Site.php @@ -3,7 +3,6 @@ namespace Formwork\Core; use Formwork\Utils\FileSystem; -use Formwork\Utils\Header; use Formwork\Utils\HTTPRequest; class Site extends AbstractPage @@ -175,7 +174,6 @@ public function errorPage($render = false) $errorPage = $this->findPage(Formwork::instance()->option('pages.error')); if ($render) { $this->currentPage = $errorPage; - Header::status(404); $errorPage->render(); exit; }