public function url($name, $params = []) { if (!isset($this->_named_routes[$name])) { throw new InvalidRouteException('Route :name doesnt exist', [':name' => $name]); } $route = $this->_routes_list[$this->_named_routes[$name]]; if ($route['is_static']) { // This is a static route, no need to replace anything // TODO: host? return '/' . $route['pattern']; } // Keep track of whether an optional param was replaced $provided_optional = FALSE; $uri = $route['pattern']; while (preg_match('#\\([^()]++\\)#', $uri, $match)) { // Search for the matched value $search = $match[0]; // Remove the parenthesis from the match as the replace $replace = substr($match[0], 1, -1); while (preg_match('#' . static::REGEX_KEY . '#', $replace, $match)) { list($key, $param) = $match; if (isset($params[$param]) and $params[$param] !== Arr::get($this->_defaults, $param)) { // Future optional params should be required $provided_optional = TRUE; // Replace the key with the parameter value $replace = str_replace($key, $params[$param], $replace); } elseif ($provided_optional) { // Look for a default if (isset($this->_defaults[$param])) { $replace = str_replace($key, $this->_defaults[$param], $replace); } else { // Ungrouped parameters are required throw new InvalidRouteException('Required route parameter not passed: :param', [':param' => $param]); } } else { // This group has missing parameters $replace = ''; break; } } // Replace the group in the URI $uri = str_replace($search, $replace, $uri); } while (preg_match('#' . static::REGEX_KEY . '#', $uri, $match)) { list($key, $param) = $match; if (!isset($params[$param])) { // Look for a default if (isset($this->_defaults[$param])) { $params[$param] = $this->_defaults[$param]; } else { // Ungrouped parameters are required throw new InvalidRouteException('Required route parameter not passed: :param', [':param' => $param]); } } $uri = str_replace($key, $params[$param], $uri); } // Trim all extra slashes from the URI $uri = preg_replace('#//+#', '/', rtrim($uri, '/')); /*if ($this->is_external()) { // Need to add the host to the URI $host = $this->_defaults['host']; if (strpos($host, '://') === FALSE) { // Use the default defined protocol $host = Route::$default_protocol.$host; } // Clean up the host and prepend it to the URI $uri = rtrim($host, '/').'/'.$uri; }*/ return '/' . $uri; }