_routes[] = array($path,$to); } function getRoute($path) { $returnroute = ''; /* loop through each route in the routes file and find the first match */ foreach ($this->_routes as $route) { $matches = true; $part = ''; $key = -1; /* loop through each token in the route and compare to each token in the requested URI */ foreach ($route[0] as $key=>$part) { if ($part == '*') { break; } if ($part == '?') { break; } if (!isset($path[$key])) { $matches = false; break; } if (preg_match('/^\/[^\/]*\/$/',$part)) { // it's a regex if (!preg_match($part,$path[$key])) { $matches = false; break; } } else { if ($part != $path[$key]) { $matches = false; break; } } } if ($part != '*' && isset($path[$key+1])) { $matches = false; } if ($matches) { // this route matches, so return it $returnroute = array(); $controller = $route[1]['controller']; $action = $route[1]['action']; foreach ($path as $k=>$val) { $controller = str_replace('{'.$k.'}',$val,$controller); $action = str_replace('{'.$k.'}',$val,$action); } $returnroute = array('controller'=>$controller,'action'=>$action,'params'=>$path); } } if ($returnroute == '') { $returnroute = array( 'controller'=>$path[0], 'action'=>(isset($path[1]) ? $path[1] : 'index'), 'params'=>$path ); } return $returnroute; } } ?>