Skip to content

Instantly share code, notes, and snippets.

@jacobch
Created February 22, 2011 22:34
Show Gist options
  • Save jacobch/839577 to your computer and use it in GitHub Desktop.
Save jacobch/839577 to your computer and use it in GitHub Desktop.
<?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