Updated router pattern matching.

pull/25/head
Mike Cao 12 years ago
parent 58303b2ae3
commit 61ce37ca64

@ -71,7 +71,7 @@ class Router {
} }
/** /**
* Tries to match a requst to a route. Also parses named parameters in the url. * Tries to match a request to a route. Also parses named parameters in the url.
* *
* @param string $pattern URL pattern * @param string $pattern URL pattern
* @param string $url Requested URL * @param string $url Requested URL
@ -81,27 +81,36 @@ class Router {
$ids = array(); $ids = array();
// Build the regex for matching // Build the regex for matching
$regex = '/^'.implode('\/', array_map( $regex = preg_replace_callback(
function($str) use (&$ids){ '#@([\w]+)(:([^/\(\)]*))?#',
if ($str == '*') { function($matches) use (&$ids) {
$str = '(.*)'; $ids[$matches[1]] = null;
if (isset($matches[3])) {
return '(?P<'.$matches[1].'>'.$matches[3].')';
} }
else if ($str != null && $str{0} == '@') { return '(?P<'.$matches[1].'>[^/\?]+)';
if (preg_match('/@(\w+)(\:([^\/|\(]*))?([\(|\)]+)?/', $str, $matches)) {
$ids[$matches[1]] = null;
return '(?P<'.$matches[1].'>'
.(!empty($matches[3]) ? $matches[3] : '[^(\/|\?)]+')
.')'
.(!empty($matches[4]) ? str_replace(')',')?',$matches[4]) : '');
}
}
return $str;
}, },
explode('/', $pattern) $pattern
)).'\/?(?:\?.*)?$/i'; );
// Allow trailing slash
if (substr($pattern, -1) === '/') {
$regex .= '?';
}
else {
$regex .= '/?';
}
// Replace wildcard
if (substr($pattern, -1) === '*') {
$regex = str_replace('*', '.+?', $pattern);
}
// Convert optional parameters
$regex = str_replace(')', ')?', $regex);
// Attempt to match route and named parameters // Attempt to match route and named parameters
if (preg_match($regex, $url, $matches)) { if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) {
foreach ($ids as $k => $v) { foreach ($ids as $k => $v) {
$this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null; $this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null;
} }

Loading…
Cancel
Save