Parse optional parameters before regex.

pull/25/head
Mike Cao 12 years ago
parent 1ba8770da7
commit 65807a1f29

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

Loading…
Cancel
Save