Commit 80c746ed authored by Taylor Otwell's avatar Taylor Otwell

Refactor route parameter parsing.

parent b5e0eb02
...@@ -44,13 +44,7 @@ class Router { ...@@ -44,13 +44,7 @@ class Router {
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 @@ class Router { ...@@ -92,23 +86,13 @@ class Router {
* *
* 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;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment