public function testParseUrl() { $result = Route::parseUrl('hello'); $this->assertEquals(['hello', null, null], $result['module']); $result = Route::parseUrl('index/hello'); $this->assertEquals(['index', 'hello', null], $result['module']); $result = Route::parseUrl('index/hello?name=thinkphp'); $this->assertEquals(['index', 'hello', null], $result['module']); $result = Route::parseUrl('index/user/hello'); $this->assertEquals(['index', 'user', 'hello'], $result['module']); $result = Route::parseUrl('index/user/hello/name/thinkphp'); $this->assertEquals(['index', 'user', 'hello'], $result['module']); $result = Route::parseUrl('index-index-hello', '-'); $this->assertEquals(['index', 'index', 'hello'], $result['module']); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param \think\Request $request * @param array $config * @throws Exception */ public static function route($request, array $config) { define('__INFO__', $request->pathinfo()); define('__EXT__', $request->ext()); // 检测URL禁用后缀 if ($config['url_deny_suffix'] && preg_match('/\\.(' . $config['url_deny_suffix'] . ')$/i', __INFO__)) { throw new Exception('url suffix deny'); } $_SERVER['PATH_INFO'] = $request->path(); $depr = $config['pathinfo_depr']; $result = false; // 路由检测 if (APP_ROUTE_ON && !empty($config['url_route_on'])) { // 开启路由 if (!empty($config['route'])) { // 导入路由配置 Route::import($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); if (APP_ROUTE_MUST && false === $result && $config['url_route_must']) { // 路由无效 throw new Exception('route not define '); } } if (false === $result) { // 路由无效默认分析为模块/控制器/操作/参数...方式URL $result = Route::parseUrl($_SERVER['PATH_INFO'], $depr); } //保证$_REQUEST正常取值 $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); // 注册调度机制 return $request->dispatch($result); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param \think\Request $request * @param array $config * @return array * @throws \think\Exception */ public static function routeCheck($request, array $config) { $path = $request->path(); $depr = $config['pathinfo_depr']; $result = false; // 路由检测 $check = !is_null(self::$routeCheck) ? self::$routeCheck : $config['url_route_on']; if ($check) { // 开启路由 if (!empty($config['route'])) { // 导入路由配置 Route::import($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $path, $depr, $config['url_domain_deploy']); $must = !is_null(self::$routeMust) ? self::$routeMust : $config['url_route_must']; if ($must && false === $result) { // 路由无效 throw new HttpException(404, 'Route Not Found'); } } if (false === $result) { // 路由无效 解析模块/控制器/操作/参数... 支持控制器自动搜索 $result = Route::parseUrl($path, $depr, $config['controller_auto_search']); } return $result; }
public function testBind() { Route::bind('module', 'index/blog'); $this->assertEquals(['type' => 'module', 'module' => ['index', 'blog', 'read']], Route::parseUrl('read/10')); Route::get('index/blog/:id', 'index/blog/read'); $this->assertEquals(['type' => 'module', 'module' => ['index', 'blog', 'read']], Route::check('10')); Route::bind('namespace', '\\app\\index\\controller'); $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read'], 'params' => []], Route::check('blog/read')); Route::bind('class', '\\app\\index\\controller\\blog'); $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read'], 'params' => []], Route::check('read')); }
/** * URL路由检测(根据PATH_INFO) * @access public * @param \think\Request $request * @param array $config * @return array * @throws \think\Exception */ public static function routeCheck($request, array $config) { $path = $request->path(); $depr = $config['pathinfo_depr']; $result = false; // 路由检测 $check = !is_null(self::$routeCheck) ? self::$routeCheck : $config['url_route_on']; if ($check) { // 开启路由 if (is_file(RUNTIME_PATH . 'route.php')) { // 读取路由缓存 $rules = (include RUNTIME_PATH . 'route.php'); if (is_array($rules)) { Route::rules($rules); } } else { $files = $config['route_config_file']; foreach ($files as $file) { if (is_file(CONF_PATH . $file . CONF_EXT)) { // 导入路由配置 $rules = (include CONF_PATH . $file . CONF_EXT); if (is_array($rules)) { Route::import($rules); } } } } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $path, $depr, $config['url_domain_deploy']); $must = !is_null(self::$routeMust) ? self::$routeMust : $config['url_route_must']; if ($must && false === $result) { // 路由无效 throw new RouteNotFoundException(); } } if (false === $result) { // 路由无效 解析模块/控制器/操作/参数... 支持控制器自动搜索 $result = Route::parseUrl($path, $depr, $config['controller_auto_search']); } return $result; }
public function testBind() { $request = Request::instance(); Route::bind('index/blog'); Route::get('blog/:id', 'index/blog/read'); $result = Route::check($request, 'blog/10'); $this->assertEquals(['index', 'blog', 'read'], $result['module']); $result = Route::parseUrl('test'); $this->assertEquals(['index', 'blog', 'test'], $result['module']); Route::bind('\\app\\index\\controller', 'namespace'); $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read']], Route::check($request, 'blog/read')); Route::bind('\\app\\index\\controller\\blog', 'class'); $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read']], Route::check($request, 'read')); }