コード例 #1
0
 private static function routerCheck()
 {
     if (file_exists_case(CONFIG_PATH . 'routes.php')) {
         $routes = (include CONFIG_PATH . 'routes.php');
         if (!is_array($routes)) {
             $routes = $_routes;
         }
         if (isset($_GET[C('VAR_ROUTER')])) {
             $routeName = $_GET[C('VAR_ROUTER')];
             unset($_GET[C('VAR_ROUTER')]);
         } else {
             $paths = explode(C('PATH_DEPR'), trim($_SERVER['PATH_INFO'], '/'));
             $routeName = array_shift($paths);
         }
         if (isset($routes[$routeName])) {
             $route = $routes[$routeName];
             $_GET[C('VAR_MODULE')] = $route[0];
             $_GET[C('VAR_ACTION')] = $route[1];
             if (!isset($_GET[C('VAR_ROUTER')])) {
                 $vars = explode(',', $route[2]);
                 for ($i = 0; $i < count($vars); $i++) {
                     $_GET[$vars[$i]] = array_shift($paths);
                 }
                 $res = preg_replace('@(\\w+)\\/([^,\\/]+)@e', '$_GET[\'\\1\']="\\2";', implode('/', $paths));
             }
             if (isset($route[3])) {
                 parse_str($route[3], $params);
                 $_GET = array_merge($_GET, $params);
             }
             unset($_SERVER['PATH_INFO']);
         } elseif (isset($routes[$routeName . '@'])) {
             $routeItem = $routes[$routeName . '@'];
             $regx = str_replace($routeName, '', trim($_SERVER['PATH_INFO'], '/'));
             foreach ($routeItem as $route) {
                 $rule = $route[0];
                 if (preg_match($rule, $regx, $matches)) {
                     $_GET[C('VAR_MODULE')] = $route[1];
                     $_GET[C('VAR_ACTION')] = $route[2];
                     if (!isset($_GET[C('VAR_ROUTER')])) {
                         $vars = explode(',', $route[3]);
                         for ($i = 0; $i < count($vars); $i++) {
                             $_GET[$vars[$i]] = $matches[$i + 1];
                         }
                         $res = preg_replace('@(\\w+)\\/([^,\\/]+)@e', '$_GET[\'\\1\']="\\2";', str_replace($matches[0], '', $regx));
                     }
                     if (isset($route[4])) {
                         parse_str($route[4], $params);
                         $_GET = array_merge($_GET, $params);
                     }
                     //unset($_SERVER['PATH_INFO']);
                     self::$useRoute = true;
                     break;
                 }
             }
         }
     }
 }
コード例 #2
0
 /**
 +----------------------------------------------------------
 * 路由检测
 +----------------------------------------------------------
 * @access private 
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
 private static function routerCheck()
 {
     // 搜索路由映射 把路由名称解析为对应的模块和操作
     if (file_exists(CONFIG_PATH . 'routes.php')) {
         $routes = (include CONFIG_PATH . 'routes.php');
         if (!is_array($routes)) {
             $routes = $_routes;
         }
         if (isset($_GET[C('VAR_ROUTER')])) {
             // 存在路由变量
             $routeName = $_GET[C('VAR_ROUTER')];
         } else {
             $paths = explode(C('PATH_DEPR'), trim($_SERVER['PATH_INFO'], '/'));
             // 获取路由名称
             $routeName = array_shift($paths);
         }
         if (isset($routes[$routeName])) {
             // 读取当前路由名称的路由规则
             // 路由定义格式 routeName=>array(‘模块名称’,’操作名称’,’参数定义’,’额外参数’)
             $route = $routes[$routeName];
             $_GET[C('VAR_MODULE')] = $route[0];
             $_GET[C('VAR_ACTION')] = $route[1];
             //	获取当前路由参数对应的变量
             if (!isset($_GET[C('VAR_ROUTER')])) {
                 $vars = explode(',', $route[2]);
                 for ($i = 0; $i < count($vars); $i++) {
                     $_GET[$vars[$i]] = array_shift($paths);
                 }
                 // 解析剩余的URL参数
                 $res = preg_replace('@(\\w+)\\/([^,\\/]+)@e', '$_GET[\'\\1\']="\\2";', implode('/', $paths));
             }
             if (isset($route[3])) {
                 // 路由里面本身包含固定参数 形式为 a=111&b=222
                 parse_str($route[3], $params);
                 $_GET = array_merge($_GET, $params);
             }
             unset($_SERVER['PATH_INFO']);
         } elseif (isset($routes[$routeName . '@'])) {
             // 存在泛路由
             // 路由定义格式 routeName@=>array(
             // array('路由正则1',‘模块名称’,’操作名称’,’参数定义’,’额外参数’),
             // array('路由正则2',‘模块名称’,’操作名称’,’参数定义’,’额外参数’),
             // ...)
             $routeItem = $routes[$routeName . '@'];
             $regx = str_replace($routeName, '', trim($_SERVER['PATH_INFO'], '/'));
             foreach ($routeItem as $route) {
                 $rule = $route[0];
                 // 路由正则
                 if (preg_match($rule, $regx, $matches)) {
                     // 匹配路由定义
                     $_GET[C('VAR_MODULE')] = $route[1];
                     $_GET[C('VAR_ACTION')] = $route[2];
                     //	获取当前路由参数对应的变量
                     if (!isset($_GET[C('VAR_ROUTER')])) {
                         $vars = explode(',', $route[3]);
                         for ($i = 0; $i < count($vars); $i++) {
                             $_GET[$vars[$i]] = $matches[$i + 1];
                         }
                         // 解析剩余的URL参数
                         $res = preg_replace('@(\\w+)\\/([^,\\/]+)@e', '$_GET[\'\\1\']="\\2";', str_replace($matches[0], '', $regx));
                     }
                     if (isset($route[4])) {
                         // 路由里面本身包含固定参数 形式为 a=111&b=222
                         parse_str($route[4], $params);
                         $_GET = array_merge($_GET, $params);
                     }
                     //unset($_SERVER['PATH_INFO']);
                     self::$useRoute = true;
                     break;
                 }
             }
         }
     }
 }