function parseUri($route) { // example: /products/cat1/detail/123/computer-case => matched: /products/cat1/detail global $arr_mapping, $ext_mapping; $rt = removeFirst('/', $route); $rt = removeLast('/', $rt); $arr = explode ('/', $rt); for ($i = count($arr)-1; $i >= 0; $i--) { $path = ''; for ($j = 0; $j < $i; $j++) { $path .= '/'.$arr[$j]; } $testCtlName = formatControllerName($arr[$j]); $testCtlPath = $path; $testCtlFullPath = BASECTL.$testCtlPath.'/'.$testCtlName.'.php'; //echo $testCtlFullPath.'<p/>'; $path .= '/'.$arr[$j]; if (file_exists($testCtlFullPath)) { //echo 'Controller.php exists!' return array('controller'.$testCtlPath, $testCtlName, extractParams($path, $route)); } else { if (isset($arr_mapping[ $path ])) { //echo 'Controller Mapping matched!'; $retArr = $arr_mapping[ $path ]; if (isset($retArr[2])) { return $retArr; } else { return array($retArr[0], $retArr[1], extractParams($path, $route)); } } else if (isset($ext_mapping[ $path ])) { //echo 'Extension Mapping Matched!'; $retArr = $ext_mapping[ $path ]; if (isset($retArr[2])) { return $retArr; } else { return array($retArr[0], $retArr[1], extractParams($path, $route)); } } } } return null; }
function removeFirstLast($ch, $s) { $ret = removeFirst($ch, $s); $ret = removeLast($ch, $ret); return $ret; }