/** * Put a URL through the routing rules and return the routed URL. * * @param string $url The URL to route * @param boolean $rebase Should I rebase the resulting URL? False to return a relative URL to the application's * live_site and base_url, as defined in the application configuration * * @return string The routed URL */ public function route($url, $rebase = true) { // Initialise $routeResult = null; // Use the routing rules to produce a list of segments if (!empty($this->rules)) { /** @var Rule $rule */ foreach ($this->rules as $rule) { $routeResult = $rule->route($url); if (is_array($routeResult)) { break; } } } // Parse the URL into an object $uri = new Uri($url); if (!is_null($routeResult)) { // We'll replace the path and query string parameters with what the routing rule sent us $uri->setPath(implode('/', $routeResult['segments'])); $uri->setQuery($routeResult['vars']); } // Do we have to rebase? if ($rebase) { // Get the base URL $baseUrl = $this->container->appConfig->get('base_url', ''); if (empty($baseUrl)) { $baseUrl = ''; } $baseUrl = rtrim($baseUrl, '/'); if (strpos($baseUrl, 'http://') === 0 || strpos($baseUrl, 'https://') === 0) { $base = $baseUrl; } else { $base = Uri::base(false, $this->container); $base = rtrim($base, '/') . '/' . $baseUrl; } $rebaseURI = new Uri($base); // Merge the paths of the rebase and routed URIs. However if the router URI contains index.php // and the base URL ends in a .php script do not append index.php to the other .php script // (this is required for the WP integration to work properly) if (!($uri->getPath() == 'index.php' && substr($rebaseURI->getPath(), -4) == '.php')) { $rebaseURI->setPath(rtrim($rebaseURI->getPath(), '/') . '/' . $uri->getPath()); } // Merge the query string parameters of the rebase and routed URIs $vars_routed = $uri->getQuery(true); $vars_rebase = $rebaseURI->getQuery(true); $rebaseURI->setQuery(array_merge($vars_rebase, $vars_routed)); // We'll return the rebased URI $uri = $rebaseURI; } return $uri->toString(); }