Skip to content

Commit

Permalink
Add the possibility to set HTTP response status in pages frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Apr 20, 2019
1 parent c25e565 commit 0e80beb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 22 additions & 2 deletions formwork/Core/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions formwork/Core/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Formwork\Core;

use Formwork\Utils\FileSystem;
use Formwork\Utils\Header;
use Formwork\Utils\HTTPRequest;

class Site extends AbstractPage
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 0e80beb

Please sign in to comment.