/** * Dispatch URI to a page. * * @param string $url The relative URL of the page * @param bool $all * * @param bool $redirect * @return Page|null * @throws \Exception */ public function dispatch($url, $all = false, $redirect = true) { // Fetch page if there's a defined route to it. $page = isset($this->routes[$url]) ? $this->get($this->routes[$url]) : null; // Try without trailing slash if (!$page && Utils::endsWith($url, '/')) { $page = isset($this->routes[rtrim($url, '/')]) ? $this->get($this->routes[rtrim($url, '/')]) : null; } // Are we in the admin? this is important! $not_admin = !isset($this->grav['admin']); // If the page cannot be reached, look into site wide redirects, routes + wildcards if (!$all && $not_admin) { // If the page is a simple redirect, just do it. if ($redirect && $page && $page->redirect()) { $this->grav->redirectLangSafe($page->redirect()); } // fall back and check site based redirects if (!$page || $page && !$page->routable()) { /** @var Config $config */ $config = $this->grav['config']; // See if route matches one in the site configuration $route = $config->get("site.routes.{$url}"); if ($route) { $page = $this->dispatch($route, $all); } else { // Try Regex style redirects $site_redirects = $config->get("site.redirects"); if (is_array($site_redirects)) { foreach ((array) $site_redirects as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $this->grav->redirectLangSafe($found); } } catch (ErrorException $e) { $this->grav['log']->error('site.redirects: ' . $pattern . '-> ' . $e->getMessage()); } } } // Try Regex style routes $site_routes = $config->get("site.routes"); if (is_array($site_routes)) { foreach ((array) $site_routes as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $page = $this->dispatch($found, $all); } } catch (ErrorException $e) { $this->grav['log']->error('site.routes: ' . $pattern . '-> ' . $e->getMessage()); } } } } } } return $page; }
/** * Dispatch URI to a page. * * @param $url * @param bool $all * @return Page|null */ public function dispatch($url, $all = false) { // Fetch page if there's a defined route to it. $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())) { /** @var Config $config */ $config = $this->grav['config']; // See if route matches one in the site configuration $route = $config->get("site.routes.{$url}"); if ($route) { $page = $this->dispatch($route, $all); } else { // Try Regex style redirects foreach ((array) $config->get("site.redirects") as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $this->grav->redirectLangSafe($found); } } catch (ErrorException $e) { $this->grav['log']->error('site.redirects: ' . $pattern . '-> ' . $e->getMessage()); } } // Try Regex style routes foreach ((array) $config->get("site.routes") as $pattern => $replace) { $pattern = '#' . $pattern . '#'; try { $found = preg_replace($pattern, $replace, $url); if ($found != $url) { $page = $this->dispatch($found, $all); } } catch (ErrorException $e) { $this->grav['log']->error('site.routes: ' . $pattern . '-> ' . $e->getMessage()); } } } } return $page; }