Pass named parameters as arguments to callback

pull/11/head
Mike Cao 14 years ago
parent a5ea267d50
commit 9f24bc0de6

@ -106,7 +106,7 @@ You can use regular expressions in your routes:
// This will match /user/1234
});
You can also user the wildcard character `*` for matching:
You can also use the wildcard character `*` for matching:
Flight::route('/blog/*', function(){
// This will match /blog/2000/02/01
@ -116,13 +116,8 @@ You can also user the wildcard character `*` for matching:
You can specify named parameters in routes which will be passed along to your callback function.
// For the URL /bob/123, this will display
// name = bob
// id = 123
Flight::route('/@name/@id', function($params){
foreach ($params as $key => $value) {
echo "$key = $value\n";
}
Flight::route('/@name/@id', function($name, $id){
echo "hello, {$name} - {$id}!"
});
You can also include regular expressions with your named parameters by using the `:` delimiter:

@ -430,7 +430,7 @@ class Flight {
if ($result !== false) {
list($callback, $params) = $result;
self::execute($callback, $params);
self::execute($callback, array_values($params));
}
else {
self::notFound();

@ -43,7 +43,7 @@ class Router {
if ($str == '*') {
$str = '(.*)';
}
else if (@$str{0} == '@') {
else if ($str{0} == '@') {
if (preg_match('/@(\w+)(\:([^\/]*))?/', $str, $matches)) {
$ids[$matches[1]] = true;
return '(?P<'.$matches[1].'>'.(isset($matches[3]) ? $matches[3] : '[^(\/|\?)]*').')';
@ -77,7 +77,7 @@ class Router {
foreach ($routes as $pattern => $callback) {
if ($pattern === '*' || $request->url === $pattern || self::match($pattern, $request->url, $params)) {
$request->matched = $pattern;
return array($callback, array($params));
return array($callback, $params);
}
}

Loading…
Cancel
Save