Changeset 2886
- Timestamp:
- 06/24/08 01:44:33 (2 months ago)
- Files:
-
- 1 modified
-
trunk/system/libraries/Router.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/libraries/Router.php
r2737 r2886 12 12 class Router_Core { 13 13 14 protected static $routes = array();14 protected static $routes; 15 15 16 16 public static $current_uri = ''; … … 19 19 public static $url_suffix = ''; 20 20 21 public static $segments = array();22 public static $rsegments = array();23 24 public static $ directory = FALSE;25 public static $controller = FALSE;26 public static $controller_path = FALSE; 27 public static $method = FALSE;28 public static $arguments = FALSE;21 public static $segments; 22 public static $rsegments; 23 24 public static $controller; 25 public static $controller_path; 26 27 public static $method = 'index'; 28 public static $arguments = array(); 29 29 30 30 /** … … 41 41 } 42 42 43 // Load routing configuration 44 self::$routes = Config::item('routes'); 43 if (self::$routes === NULL) 44 { 45 // Load routes 46 self::$routes = Config::item('routes'); 47 } 45 48 46 49 // Default route status … … 81 84 self::$rsegments = explode('/', self::$rsegments); 82 85 83 // Prepare for Controller search 84 self::$directory = ''; 85 self::$controller = ''; 86 87 // Optimize the check for the most common controller location 88 if (is_file(APPPATH.'controllers/'.self::$rsegments[0].EXT)) 89 { 90 self::$directory = APPPATH.'controllers/'; 91 self::$controller = self::$rsegments[0]; 92 self::$method = isset(self::$rsegments[1]) ? self::$rsegments[1] : 'index'; 93 self::$arguments = isset(self::$rsegments[2]) ? array_slice(self::$rsegments, 2) : array(); 94 } 95 else 96 { 97 // Fetch the include paths 98 $include_paths = Config::include_paths(); 99 100 // Path to be added to as we search deeper 101 $search = 'controllers'; 102 103 // controller path to be added to as we search deeper 104 $controller_path = ''; 105 106 // Use the rsegments to find the controller 107 foreach (self::$rsegments as $key => $segment) 108 { 109 foreach ($include_paths as $path) 110 { 111 // The controller has been found, all arguments can be set 112 if (is_file($path.$search.'/'.$segment.EXT)) 113 { 114 self::$directory = $path.$search.'/'; 115 self::$controller_path = $controller_path; 116 self::$controller = $segment; 117 self::$method = isset(self::$rsegments[$key + 1]) ? self::$rsegments[$key + 1] : 'index'; 118 self::$arguments = isset(self::$rsegments[$key + 2]) ? array_slice(self::$rsegments, $key + 2) : array(); 119 120 // Stop searching, two levels 121 break 2; 122 } 123 } 124 125 // Add the segment to the search 126 $search .= '/'.$segment; 127 $controller_path .= $segment.'/'; 86 // Prepare to find the controller 87 $controller_path = ''; 88 89 foreach (self::$rsegments as $key => $segment) 90 { 91 // Add the segment to the search path 92 $controller_path .= $segment; 93 94 if ($path = Kohana::find_file('controllers', $controller_path, FALSE)) 95 { 96 // Set controller name 97 self::$controller = $segment; 98 99 // Change controller path 100 self::$controller_path = $path; 101 } 102 else 103 { 104 // Stop searching 105 break; 106 } 107 108 // Add another slash 109 $controller_path .= '/'; 110 } 111 112 if (self::$controller !== NULL AND isset(self::$rsegments[$key])) 113 { 114 // Set method 115 self::$method = self::$rsegments[$key]; 116 117 if (isset(self::$rsegments[$key + 1])) 118 { 119 // Set arguments 120 self::$arguments = array_slice(self::$rsegments, $key + 1); 128 121 } 129 122 } … … 132 125 Event::run('system.post_routing'); 133 126 134 if ( empty(self::$controller))127 if (self::$controller === NULL) 135 128 { 136 129 // No controller was found, so no page can be rendered … … 211 204 { 212 205 // Remove the URL suffix 213 self::$current_uri = preg_replace(' !'.preg_quote($suffix).'$!u', '', self::$current_uri);206 self::$current_uri = preg_replace('#'.preg_quote($suffix).'$#u', '', self::$current_uri); 214 207 215 208 // Set the URL suffix … … 218 211 219 212 // Reduce multiple slashes into single slashes 220 self::$current_uri = preg_replace(' !//+!', '/', self::$current_uri);213 self::$current_uri = preg_replace('#//+#', '/', self::$current_uri); 221 214 } 222 215 } … … 230 223 public static function routed_uri($uri) 231 224 { 232 $routes = Config::item('routes'); 233 $uri = $routed_uri = trim($uri, '/'); 234 235 if (isset($routes[$uri])) 225 if (self::$routes === NULL) 226 { 227 // Load routes 228 self::$routes = Config::item('routes'); 229 } 230 231 // Prepare variables 232 $routed_uri = $uri = trim($uri, '/'); 233 234 if (isset(self::$routes[$uri])) 236 235 { 237 236 // Literal match, no need for regex 238 $routed_uri = $routes[$uri];237 $routed_uri = self::$routes[$uri]; 239 238 } 240 239 else 241 240 { 242 241 // Loop through the routes and see if anything matches 243 foreach ( $routes as $key => $val)242 foreach (self::$routes as $key => $val) 244 243 { 245 244 if ($key === '_default') continue; … … 249 248 $val = trim($val, '/'); 250 249 251 // Does this route match the current URI?252 250 if (preg_match('#^'.$key.'$#u', $uri)) 253 251 { 254 // If the regex contains a valid callback, we'll use it255 252 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) 256 253 { 254 // Use regex routing 257 255 $routed_uri = preg_replace('#^'.$key.'$#u', $val, $uri); 258 256 } 259 257 else 260 258 { 259 // Standard routing 261 260 $routed_uri = $val; 262 261 } 263 262 264 // A valid route was found, stop parsing other routes263 // A valid route has been found 265 264 break; 266 265 } … … 268 267 } 269 268 270 // Check router one more time to do some magic271 if (isset($routes[$routed_uri]))272 {273 $routed_uri = $routes[$routed_uri];274 } 275 276 return $routed_uri;269 if (isset(self::$routes[$routed_uri])) 270 { 271 // Check for double routing (without regex) 272 $routed_uri = self::$routes[$routed_uri]; 273 } 274 275 return trim($routed_uri, '/'); 277 276 } 278 277
