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) {
$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)) {

Loading…
Cancel
Save