From 9f24bc0de666b44ea3e4d9c9ec9c648220fda4db Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sat, 2 Apr 2011 06:04:06 +0000 Subject: [PATCH] Pass named parameters as arguments to callback --- README.md | 11 +++-------- flight/Flight.php | 2 +- flight/Router.php | 4 ++-- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f2e61a3..0fbb202 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/flight/Flight.php b/flight/Flight.php index 10f7f80..84a239b 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -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(); diff --git a/flight/Router.php b/flight/Router.php index bac7e8e..2326e18 100644 --- a/flight/Router.php +++ b/flight/Router.php @@ -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); } }