From 79f5aaa03239c0f55112a2093f0d5dac6009946e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Sat, 4 Jul 2015 10:11:40 -0600 Subject: [PATCH] added a default route alias - particularly useful for multilane switching --- system/src/Grav/Common/Page/Page.php | 22 +++++++++++++++++++--- system/src/Grav/Common/Page/Pages.php | 18 +++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index d8a969478..af01425dd 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -47,6 +47,7 @@ class Page protected $unpublish_date; protected $slug; protected $route; + protected $raw_route; protected $url; protected $routes; protected $routable; @@ -115,8 +116,6 @@ class Page $this->header(); $this->date(); $this->metadata(); - $this->slug(); - $this->route(); $this->url(); $this->visible(); $this->modularTwig($this->slug[0] == '_'); @@ -1174,7 +1173,6 @@ class Page } if (empty($this->route)) { - // calculate route based on parent slugs $baseRoute = $this->parent ? (string) $this->parent()->route() : null; $this->route = isset($baseRoute) ? $baseRoute . '/'. $this->slug() : null; @@ -1189,6 +1187,24 @@ class Page return $this->route; } + public function rawRoute($var = null) + { + if ($var !== null) { + $this->raw_route = $var; + } + + if (empty($this->raw_route)) { + $baseRoute = $this->parent ? (string) $this->parent()->rawRoute() : null; + + $regex = '/^[0-9]+\./u'; + $slug = preg_replace($regex, '', $this->folder); + + $this->raw_route = isset($baseRoute) ? $baseRoute . '/'. $slug : null; + } + + return $this->raw_route; + } + /** * Gets the route aliases for the page based on page headers. * diff --git a/system/src/Grav/Common/Page/Pages.php b/system/src/Grav/Common/Page/Pages.php index 47a96ef18..5b1cf143e 100644 --- a/system/src/Grav/Common/Page/Pages.php +++ b/system/src/Grav/Common/Page/Pages.php @@ -696,21 +696,29 @@ class Pages // process taxonomy $taxonomy->addTaxonomy($page); - // add route $route = $page->route(); - $this->routes[$route] = $page->path(); + $raw_route = $page->rawRoute(); + $page_path = $page->path(); - // add canonical + // add regular route + $this->routes[$route] = $page_path; + + // add raw route + if ($raw_route != $route) { + $this->routes[$raw_route] = $page_path; + } + + // add canonical route $route_canonical = $page->routeCanonical(); if ($route_canonical && ($route !== $route_canonical)) { - $this->routes[$route_canonical] = $page->path(); + $this->routes[$route_canonical] = $page_path; } // add aliases to routes list if they are provided $route_aliases = $page->routeAliases(); if ($route_aliases) { foreach ($route_aliases as $alias) { - $this->routes[$alias] = $page->path(); + $this->routes[$alias] = $page_path; } } }