From bef9e3c5ce124e2f7c97e22cda0496b95acaaa6d Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Fri, 11 Sep 2015 10:21:25 -0600 Subject: [PATCH] Add simple `redirect:` header option for Page --- system/src/Grav/Common/Page/Page.php | 20 +++++++++++++++++++- system/src/Grav/Common/Page/Pages.php | 10 ++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index e0e969a79..71507d1aa 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -42,6 +42,7 @@ class Page protected $path; protected $extension; + protected $id; protected $parent; protected $template; protected $expires; @@ -56,7 +57,7 @@ class Page protected $routes; protected $routable; protected $modified; - protected $id; + protected $redirect; protected $items; protected $header; protected $frontmatter; @@ -303,6 +304,9 @@ class Page if (isset($this->header->visible)) { $this->visible = (bool) $this->header->visible; } + if (isset($this->header->redirect)) { + $this->redirect = trim($this->header->redirect); + } if (isset($this->header->order_dir)) { $this->order_dir = trim($this->header->order_dir); } @@ -1393,6 +1397,20 @@ class Page return $this->modified; } + /** + * Gets the redirect set in the header. + * + * @param string $var redirect url + * @return array + */ + public function redirect($var = null) + { + if ($var !== null) { + $this->redirect = $var; + } + return $this->redirect; + } + /** * Gets and sets the option to show the etag header for the page. * diff --git a/system/src/Grav/Common/Page/Pages.php b/system/src/Grav/Common/Page/Pages.php index 19c654b6a..f025312ea 100644 --- a/system/src/Grav/Common/Page/Pages.php +++ b/system/src/Grav/Common/Page/Pages.php @@ -271,7 +271,13 @@ class Pages $page = isset($this->routes[$url]) ? $this->get($this->routes[$url]) : null; // If the page cannot be reached, look into site wide redirects, routes + wildcards - if (!$all && (!$page || !$page->routable())) { + if (!$all && (!$page || !$page->routable() || $page->redirect())) { + + // If the page is a simple redirect, just do it. + if ($page->redirect()) { + $this->grav->redirectLangSafe($page->redirect()); + } + /** @var Config $config */ $config = $this->grav['config']; @@ -289,7 +295,7 @@ class Pages $this->grav->redirectLangSafe($found); } } catch (ErrorException $e) { - $this->grav['log']->error('site.redirects: '. $pattern . '-> ' . $e->getMessage()); + $this->grav['log']->error('site.redirects: ' . $pattern . '-> ' . $e->getMessage()); } }