From 5a67571020d605c92213442c264c67bbd82805ba Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 7 Jul 2011 12:17:55 -0700 Subject: [PATCH] Refactoring the Router class. --- system/router.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/system/router.php b/system/router.php index c134828b..a2226a83 100644 --- a/system/router.php +++ b/system/router.php @@ -18,18 +18,19 @@ class Router { */ 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)) { 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? - 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) @@ -42,7 +43,7 @@ public static function route($method, $uri) { $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)); } @@ -70,7 +71,7 @@ public static function load($uri) } else { - $segments = explode('/', trim($uri, '/')); + $segments = explode('/', $uri); if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT)) {