diff --git a/flight/net/Router.php b/flight/net/Router.php index 2d4d454..a344484 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -87,6 +87,9 @@ class Router { public function match($pattern, $url) { $ids = array(); + // Convert optional parameters + $pattern = str_replace(')', ')?', $pattern); + // Build the regex for matching $regex = preg_replace_callback( '#@([\w]+)(:([^/\(\)]*))?#', @@ -100,21 +103,18 @@ class Router { $pattern ); - // Allow trailing slash + // Fix trailing slash if (substr($pattern, -1) === '/') { $regex .= '?'; } - else { - $regex .= '/?'; - } - // Replace wildcard - if (substr($pattern, -1) === '*') { + else if (substr($pattern, -1) === '*') { $regex = str_replace('*', '.+?', $pattern); } - - // Convert optional parameters - $regex = str_replace(')', ')?', $regex); + // Allow trailing slash + else { + $regex .= '/?'; + } // Attempt to match route and named parameters if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) {