Commit 5a675710 authored by Taylor Otwell's avatar Taylor Otwell

Refactoring the Router class.

parent 94ccbffb
...@@ -18,18 +18,19 @@ class Router { ...@@ -18,18 +18,19 @@ class Router {
*/ */
public static function route($method, $uri) public static function route($method, $uri)
{ {
// Prepend a forward slash since all routes begin with one.
$uri = ($uri != '/') ? '/'.$uri : $uri;
if (is_null(static::$routes)) if (is_null(static::$routes))
{ {
static::$routes = (is_dir(APP_PATH.'routes')) ? static::load($uri) : require APP_PATH.'routes'.EXT; static::$routes = (is_dir(APP_PATH.'routes')) ? static::load($uri) : require APP_PATH.'routes'.EXT;
} }
// Put the request method and URI in route form. All routes begin with
// the request method and a forward slash.
$uri = $method.' /'.trim($uri, '/');
// Is there an exact match for the request? // Is there an exact match for the request?
if (isset(static::$routes[$method.' '.$uri])) if (isset(static::$routes[$uri]))
{ {
return Request::$route = new Route($method.' '.$uri, static::$routes[$method.' '.$uri]); return Request::$route = new Route($uri, static::$routes[$uri]);
} }
foreach (static::$routes as $keys => $callback) foreach (static::$routes as $keys => $callback)
...@@ -42,7 +43,7 @@ class Router { ...@@ -42,7 +43,7 @@ class Router {
{ {
$key = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $key)); $key = str_replace(':num', '[0-9]+', str_replace(':any', '[a-zA-Z0-9\-_]+', $key));
if (preg_match('#^'.$key.'$#', $method.' '.$uri)) if (preg_match('#^'.$key.'$#', $uri))
{ {
return Request::$route = new Route($keys, $callback, static::parameters($uri, $key)); return Request::$route = new Route($keys, $callback, static::parameters($uri, $key));
} }
...@@ -70,7 +71,7 @@ class Router { ...@@ -70,7 +71,7 @@ class Router {
} }
else else
{ {
$segments = explode('/', trim($uri, '/')); $segments = explode('/', $uri);
if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT)) if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT))
{ {
......
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