think\Route::parseUrl PHP Method

parseUrl() public static method

..
public static parseUrl ( string $url, string $depr = '/', boolean $autoSearch = false ) : array
$url string URL地址
$depr string URL分隔符
$autoSearch boolean 是否自动深度搜索控制器
return array
    public static function parseUrl($url, $depr = '/', $autoSearch = false)
    {
        if (isset(self::$bind['module'])) {
            // 如果有模块/控制器绑定
            $url = self::$bind['module'] . '/' . ltrim($url, '/');
        }
        $url = str_replace($depr, '|', $url);
        list($path, $var) = self::parseUrlPath($url);
        $route = [null, null, null];
        if (isset($path)) {
            // 解析模块
            $module = Config::get('app_multi_module') ? array_shift($path) : null;
            if ($autoSearch) {
                // 自动搜索控制器
                $dir = APP_PATH . ($module ? $module . DS : '') . Config::get('url_controller_layer');
                $suffix = App::$suffix || Config::get('controller_suffix') ? ucfirst(Config::get('url_controller_layer')) : '';
                $item = [];
                foreach ($path as $val) {
                    $item[] = array_shift($path);
                    if (is_file($dir . DS . $val . $suffix . EXT)) {
                        break;
                    } else {
                        $dir .= DS . $val;
                    }
                }
                $controller = implode('.', $item);
            } else {
                // 解析控制器
                $controller = !empty($path) ? array_shift($path) : null;
            }
            // 解析操作
            $action = !empty($path) ? array_shift($path) : null;
            // 解析额外参数
            self::parseUrlParams(empty($path) ? '' : implode('|', $path));
            // 封装路由
            $route = [$module, $controller, $action];
            if (isset(self::$rules['name'][implode($depr, $route)])) {
                throw new HttpException(404, 'invalid request:' . $url);
            }
        }
        return ['type' => 'module', 'module' => $route];
    }

Usage Example

Example #1
0
 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']);
 }
All Usage Examples Of think\Route::parseUrl