Created
February 22, 2011 22:34
-
-
Save jacobch/839577 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Route { | |
private static $routes = array(); | |
public static function set($regex, $defaults) { | |
self::$routes[] = new Route($regex, $defaults); | |
} | |
public static function match($uri) { | |
foreach(self::$routes as $route) { | |
if ($params = $route->matches($uri)) { | |
return $params; | |
} | |
} | |
return false; | |
} | |
private $regex; | |
private $defaults; | |
public function __construct($regex, $defaults) { | |
$this->regex = $regex; | |
$this->defaults = $defaults; | |
} | |
public function matches($uri) { | |
if (preg_match($this->regex, $uri, $matches)) { | |
$params = array(); | |
foreach($matches as $key => $value) { | |
if (is_int($key)) | |
continue; | |
$params[$key] = $value; | |
} | |
return array_merge($this->defaults, $params); | |
} | |
return false; | |
} | |
} | |
Route::set('~ | |
manmanagement | |
(/(?<controller>\w+))? | |
(/(?<action>\w+))? | |
(/(?<id>\d+))? | |
~x', array( | |
'directory' => 'manmanagement', | |
'controller' => 'manmanagement', | |
'action' => 'index' | |
)); | |
Route::set('~ | |
((?<controller>\w+))? | |
(/(?<action>\w+))? | |
(/(?<id>\d+))? | |
~x', array( | |
'controller' => 'login', | |
'action' => 'index' | |
)); | |
var_dump(Route::match('manmanagement/system/settings')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment