$value) { if (strlen($value) == 0) unset($array[$key]); } } private static function ruleMatch($rule, $data) { $ruleItems = explode('/',$rule); self::arrayClean($ruleItems); $dataItems = explode('/',$data); self::arrayClean($dataItems); if (count($ruleItems) == count($dataItems)) { $result = array(); foreach($ruleItems as $ruleKey => $ruleValue) { if (preg_match('/^:[\w]{1,}$/',$ruleValue)) { $ruleValue = substr($ruleValue,1); $result[$ruleValue] = $dataItems[$ruleKey]; } else { if (strcmp($ruleValue,$dataItems[$ruleKey]) != 0) { return false; } } } if (count($result) > 0) return $result; unset($result); } return false; } private static function defaultRoutes($url) { // process default routes $items = explode('/',$url); // remove empty blocks foreach($items as $key => $value) { if (strlen($value) == 0) unset($items[$key]); } // extract data if (count($items)) { self::$controller = array_shift($items); self::$action = array_shift($items); self::$params = $items; } } protected function __construct() { self::$rules = array(); } public static function init() { /*$url = $_SERVER['REQUEST_URI']; $isCustom = false; if (count(self::$rules)) { foreach(self::$rules as $ruleKey => $ruleData) { var_dump($ruleKey); var_dump($ruleData); self::$params = self::ruleMatch($ruleKey,$url); // if ($params) { self::$controller = $ruleData['controller']; self::$action = $ruleData['action']; $isCustom = true; break; // } } } if (!$isCustom) self::defaultRoutes($url); if (!strlen(self::$controller)) self::$controller = 'HomeController'; if (!strlen(self::$action)) self::$action = 'getIndex'; if(!self::$params){ $params = []; } else { $params = self::$params; }*/ //$path = trim(parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), "/"); // $path = preg_replace('/[^a-zA-Z0-9]/', "", $path); /* if (strpos($path, PATH) === 0) { $path = substr($path, strlen(PATH)); } @list($controller, $action, $params) = explode("/", $path, 3);*/ /* if (isset($controller)) { self::$controller = $controller; } if (isset($action)) { self::$action = $action; }*/ foreach(self::$rules as $k=>$r){ $pos = strpos($_SERVER["REQUEST_URI"],$k); if($pos !==false){ if(!isset($r['params'])) { self::$params = str_replace($k,'',$_SERVER["REQUEST_URI"]); self::$params= substr( $_SERVER["REQUEST_URI"], $pos + strlen($k)+1 ); self::$params = explode("/", self::$params, 3); foreach(self::$params as $k=>$p){ if($p==''){ unset(self::$params[$k]); } } } else { $prm = str_replace($k,'',$_SERVER["REQUEST_URI"]); $prm = explode("/", $prm, 3); foreach($prm as $k=>$p){ if($p==''){ unset($prm[$k]); } } self::$params = array_merge($r['params'], $prm); } self::$controller = $r['controller']; self::$action = $r['action']; break; } else { self::$controller = 'HomeController'; self::$action = 'getIndex'; self::$params = array(); } } /* var_dump(self::$controller); var_dump(self::$action); var_dump(self::$params); die();*/ call_user_func_array(array(new self::$controller, self::$action), self::$params); //$pos = strpos($_SERVER["REQUEST_URI"],'admin/login'); //self::$params = str_replace('admin/login','',$_SERVER["REQUEST_URI"]); //var_dump($params); // $c = self::$rules['/'.$controller]; //var_dump($path); //var_dump(self::$rules); //var_dump($_SERVER["REQUEST_URI"]); // var_dump($c); // die(); // call_user_func_array(array(new self::$controller, self::$action), self::$params); // call_user_func_array(array(new $c['controller'], $c['action']), self::$params); } public static function addRule($rule, $target) { self::$rules[$rule] = $target; } public static function getRules(){ return self::$rules; } public static function getController() { return self::$controller; } public static function getAction() { return self::$action; } public static function getParams() { return self::$params; } public static function getParam($id) { return self::$params[$id]; } } ?>