/** * @brief dispatch 路由分发方法 * * @return void */ public static function dispatch() { // 注册最后的通用路由 $route = array('widget' => 'Page', 'method' => 'showPage', 'format' => '/%s/', 'patter' => '|^/([^/]+)[/]?$|', 'params' => array('alias')); self::setRoute('Page', $route); // 获取地址信息 if (OptionLibrary::get('rewrite') == 'open') { $pathInfo = str_replace(substr(LOGX_PATH, 0, strlen(LOGX_PATH) - 1), '', Request::S('REQUEST_URI', 'string')); $pathInfo = str_replace('?' . Request::S('QUERY_STRING', 'string'), '', $pathInfo); } else { $pathInfo = self::getPathInfo(); } $pathInfo = Plugin::call('pathInfo', $pathInfo); // 遍历路由表进行匹配 foreach (self::$_routeTable as $key => $route) { if (preg_match($route['patter'], $pathInfo, $matches)) { self::$_currentRoute = $key; $params = NULL; if (!empty($route['params'])) { unset($matches[0]); $params = array_combine($route['params'], $matches); } self::$_currentParams = $params; if (isset($route['widget'])) { Widget::getWidget($route['widget'])->{$route}['method']($params); } elseif (isset($route['plugin'])) { Plugin::getPlugin($route['plugin'])->{$route}['method']($params); } return; } } //echo '**'.$_SERVER['QUERY_STRING']; //$path = explode( '/', $pathInfo ); // 永久重定向为规范的 URL 地址 //Response::redirect( $pathInfo.'/', true ); // 没有匹配的路由则显示 404 页面 Response::error(404); }
/** * Returns the route data associated with the current route. * * The route data is set in a modules setup.php file. If no data is * found for the current route, boolean false is returned. * * @return mixed Array of data if route exists, boolean false otherwise * * TODO Check for invalid characters in URL */ public static function getRouteData() { $data = false; /* $defaultRoute = Config::get('router.default_route'); $currentRoute = $defaultRoute; if(isset($_GET['r']) && strlen($_GET['r'])) $currentRoute = $_GET['r']; $currentRoute = rtrim($currentRoute, '/'); // Look for language code, and modify route if need be if(Multilanguage::routeHasLangCode($currentRoute)) { $currentRoute = ltrim(substr($currentRoute, 3), '/'); // If current route is empty now (due to being at base url with language code (ex: /<code>), set to default if(!strlen($currentRoute)) $currentRoute = $defaultRoute; } */ $currentRoute = Url::current(); if ($currentRoute == '/') { $currentRoute = Config::get('router.default_route'); } else { $currentRoute = ltrim($currentRoute, '/'); } // We dont want leading slash of the current url for routes while (true) { // First search routes with exact match if (isset(self::$_routes[$currentRoute])) { $data = self::$_routes[$currentRoute]; } // If no exact match, check matches with regex foreach (self::$_routes as $route => $routeData) { $regexRoute = String::regify($route); if (preg_match('@^' . $regexRoute . '$@', $currentRoute, $matches)) { self::$_params = array_slice($matches, 1); $data = $routeData; } } // If no route data was found, must be 404 if (!$data) { break; } elseif (isset($data['redirect'])) { $currentRoute = $data['redirect']; } elseif ($data) { // First check permissions, if any, for current user self::$_currentRoute = array('route' => $currentRoute, 'data' => $data); break; } } if ($data && !isset($data['permissions'])) { $data['permissions'] = array(); } // Let other areas of the application make use of route data Event::trigger('router.data', array($currentRoute, $data)); return array($currentRoute, $data); }