From 8277694693928d019784f959fad3a1fcee7f324f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sat, 9 Jul 2011 23:41:27 -0500 Subject: [PATCH] refactoring router. --- system/router.php | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/system/router.php b/system/router.php index 1a898bfd..3aa7e5d9 100644 --- a/system/router.php +++ b/system/router.php @@ -23,7 +23,8 @@ public static function route($method, $uri) static::$routes = static::load($uri); } - // Put the request method and URI in route form. Routes begin with the request method and a forward slash. + // Put the request method and URI in route form. + // Routes begin with the request method and a forward slash. $uri = $method.' /'.trim($uri, '/'); // Is there an exact match for the request? @@ -34,7 +35,8 @@ public static function route($method, $uri) foreach (static::$routes as $keys => $callback) { - // Only check routes that have multiple URIs or wildcards. Other routes would be caught by a literal match. + // Only check routes that have multiple URIs or wildcards. + // Other routes would have been caught by the check for literal matches. if (strpos($keys, '(') !== false or strpos($keys, ',') !== false ) { foreach (explode(', ', $keys) as $key) @@ -58,31 +60,29 @@ public static function route($method, $uri) */ public static function load($uri) { - if ( ! is_dir(APP_PATH.'routes')) + return (is_dir(APP_PATH.'routes')) ? static::load_from_directory($uri) : require APP_PATH.'routes'.EXT; + } + + /** + * Load the appropriate route file from the routes directory. + * + * @param string $uri + * @return array + */ + private static function load_from_directory($uri) + { + // If it exists, The "home" routes file is loaded for every request. This allows + // for "catch-all" routes such as http://example.com/username... + $home = (file_exists($path = APP_PATH.'routes/home'.EXT)) ? require $path : array(); + + if ($uri == '') { - return require APP_PATH.'routes'.EXT; + return $home; } - if ( ! file_exists(APP_PATH.'routes/home'.EXT)) - { - throw new \Exception("A [home] route file is required when using a route directory."); - } + $segments = explode('/', $uri); - if ($uri == '/') - { - return require APP_PATH.'routes/home'.EXT; - } - else - { - $segments = explode('/', $uri); - - if ( ! file_exists(APP_PATH.'routes/'.$segments[0].EXT)) - { - return require APP_PATH.'routes/home'.EXT; - } - - return array_merge(require APP_PATH.'routes/'.$segments[0].EXT, require APP_PATH.'routes/home'.EXT); - } + return (file_exists($path = APP_PATH.'routes/'.$segments[0].EXT)) ? array_merge(require $path, $home) : $home; } /**