From 80c746edb2bc959b4a46d22bdd17a3220107eeab Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 11:38:30 -0700 Subject: [PATCH] Refactor route parameter parsing. --- system/router.php | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/system/router.php b/system/router.php index ace47dc3..5ac5c765 100644 --- a/system/router.php +++ b/system/router.php @@ -44,13 +44,7 @@ public static function route($method, $uri) if (preg_match('#^'.$key.'$#', $method.' '.$uri)) { - // Remove the leading slashes from the route and request URIs. Also trim - // the request method off of the route URI. This should get the request - // and route URIs in the same format so we can extract the parameters. - $uri = trim($uri, '/'); - $key = trim(substr($key, strlen($method.' ')), '/'); - - return Request::$route = new Route($keys, $callback, static::parameters(explode('/', $uri), explode('/', $key))); + return Request::$route = new Route($keys, $callback, static::parameters($uri, $key)); } } } @@ -92,23 +86,13 @@ public static function load($uri) * * Any route segment wrapped in parentheses is considered a parameter. * - * @param array $uri - * @param array $route + * @param string $uri + * @param string $route * @return array */ private static function parameters($uri, $route) { - $parameters = array(); - - for ($i = 0; $i < count($route); $i++) - { - if (strpos($route[$i], '(') === 0) - { - $parameters[] = $uri[$i]; - } - } - - return $parameters; + return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route)))); } } \ No newline at end of file