Refactor route parameter parsing.

This commit is contained in:
Taylor Otwell 2011-07-07 11:38:30 -07:00
parent b5e0eb022c
commit 80c746edb2
1 changed files with 4 additions and 20 deletions

View File

@ -44,13 +44,7 @@ public static function route($method, $uri)
if (preg_match('#^'.$key.'$#', $method.' '.$uri)) if (preg_match('#^'.$key.'$#', $method.' '.$uri))
{ {
// Remove the leading slashes from the route and request URIs. Also trim return Request::$route = new Route($keys, $callback, static::parameters($uri, $key));
// 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)));
} }
} }
} }
@ -92,23 +86,13 @@ public static function load($uri)
* *
* Any route segment wrapped in parentheses is considered a parameter. * Any route segment wrapped in parentheses is considered a parameter.
* *
* @param array $uri * @param string $uri
* @param array $route * @param string $route
* @return array * @return array
*/ */
private static function parameters($uri, $route) private static function parameters($uri, $route)
{ {
$parameters = array(); return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route))));
for ($i = 0; $i < count($route); $i++)
{
if (strpos($route[$i], '(') === 0)
{
$parameters[] = $uri[$i];
}
}
return $parameters;
} }
} }