💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] * * * * * ## 1 Url操作文件源代码(thinkphp\library\think\Url.php) ~~~ public static function build($url = '', $vars = '', $suffix = true, $domain = false) { $info = parse_url($url); $url = !empty($info['path']) ? $info['path'] : ''; if (isset($info['fragment'])) { $anchor = $info['fragment']; if (false !== strpos($anchor, '?')) { list($anchor, $info['query']) = explode('?', $anchor, 2); } if (false !== strpos($anchor, '@')) { list($anchor, $domain) = explode('@', $anchor, 2); } } elseif (false !== strpos($url, '@')) { list($url, $domain) = explode('@', $info['path'], 2); } if (is_string($vars)) { parse_str($vars, $vars); } if (isset($info['query'])) { parse_str($info['query'], $params); $vars = array_merge($params, $vars); } $alias = self::getRouteAlias(); if (0 !== strpos($url, '/') && isset($alias[$url]) && $match = self::getRouteUrl($alias[$url], $vars)) { $url = str_replace('[--think--]', '', $match); } else { $url = self::parseUrl($url); } $type = Route::bind('type'); if ($type) { $bind = Route::bind($type); if (0 === strpos($url, $bind)) { $url = substr($url, strlen($bind) + 1); } } $depr = Config::get('pathinfo_depr'); $url = str_replace('/', $depr, $url); $suffix = ('/' == $url) ? '' : self::parseSuffix($suffix); $anchor = !empty($anchor) ? '#' . $anchor : ''; if (!empty($vars)) { if (Config::get('url_common_param')) { $vars = urldecode(http_build_query($vars)); $url .= $suffix . $anchor . '?' . $vars; } else { foreach ($vars as $var => $val) { if ('' !== trim($val)) { $url .= $depr . $var . $depr . urlencode($val); } } $url .= $suffix . $anchor; } } else { $url .= $suffix . $anchor; } $domain = self::parseDomain($url, $domain); $url = $domain . Config::get('base_url') . '/' . $url; return $url; } ~~~ ~~~ protected static function parseUrl($url) { if (0 === strpos($url, '/')) { $url = substr($url, 1); } elseif (false !== strpos($url, '\\')) { $url = ltrim(str_replace('\\', '/', $url), '/'); } elseif (0 === strpos($url, '@')) { $url = substr($url, 1); } else { $module = MODULE_NAME ? MODULE_NAME . '/' : ''; if ('' == $url) { $url = $module . CONTROLLER_NAME . '/' . ACTION_NAME; } else { $path = explode('/', $url); $len = count($path); if ($len < 3) { $url = $module . (1 == $len ? CONTROLLER_NAME . '/' : '') . $url; } } } return $url; } ~~~ ~~~ protected static function parseDomain($url, $domain) { if ($domain) { if (true === $domain) { $domain = $_SERVER['HTTP_HOST']; if (Config::get('url_domain_deploy')) { $domain = $_SERVER['HTTP_HOST']; foreach (Route::domain() as $key => $rule) { $rule = is_array($rule) ? $rule[0] : $rule; if (false === strpos($key, '*') && 0 === strpos($url, $rule)) { $domain = $key . strstr($domain, '.'); break; } } } } else { $domain .= strpos($domain, '.') ? '' : strstr($_SERVER['HTTP_HOST'], '.'); } $domain = (self::isSsl() ? 'https://' : 'http://') . $domain; } else { $domain = ''; } return $domain; } ~~~ ~~~ protected static function pattern($pattern, $vars) { foreach ($pattern as $key => $type) { if (1 == $type && !isset($vars[$key])) { return false; } } return true; } ~~~ ~~~ protected static function parseSuffix($suffix) { if ($suffix) { $suffix = true === $suffix ? Config::get('url_html_suffix') : $suffix; if ($pos = strpos($suffix, '|')) { $suffix = substr($suffix, 0, $pos); } } return (empty($suffix) || 0 === strpos($suffix, '.')) ? $suffix : '.' . $suffix; } ~~~ ~~~ public static function isSsl() { if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) { return true; } elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) { return true; } return false; } ~~~ ~~~ public static function getRouteUrl($alias, &$vars = []) { foreach ($alias as $key => $val) { list($url, $pattern) = $val; if (strpos($url, '$')) { $url = str_replace('$', '[--think--]', $url); } if (self::pattern($pattern, $vars)) { foreach ($vars as $key => $val) { if (false !== strpos($url, '[:' . $key . ']')) { $url = str_replace('[:' . $key . ']', $val, $url); unset($vars[$key]); } elseif (false !== strpos($url, ':' . $key)) { $url = str_replace(':' . $key, $val, $url); unset($vars[$key]); } } return $url; } } return false; } ~~~ ~~~ private static function getRouteAlias() { if ($alias = Cache::get('think_route_alias')) { return $alias; } $rules = Route::getRules(); if (empty($rules)) { return []; } foreach ($rules as $rule => $val) { if (!empty($val['routes'])) { foreach ($val['routes'] as $key => $route) { if (is_numeric($key)) { $key = array_shift($route); } $route = $route[0]; if (is_array($route)) { $route = implode('\\', $route); } elseif ($route instanceof \Closure) { continue; } elseif (strpos($route, '?')) { $route = strstr($route, '?', true); } $var = self::parseVar($rule . '/' . $key); $alias[$route][] = [$rule . '/' . $key, $var]; } } else { $route = $val['route']; if (is_array($route)) { $route = implode('\\', $route); } elseif ($route instanceof \Closure) { continue; } elseif (strpos($route, '?')) { $route = strstr($route, '?', true); } $var = self::parseVar($rule); $alias[$route][] = [$rule, $var]; } } Cache::set('think_route_alias', $alias); return $alias; } ~~~ ~~~ private static function parseVar($rule) { if ($depr = Config::get('url_params_depr')) { $rule = str_replace($depr, '/', $rule); } $var = []; foreach (explode('/', $rule) as $val) { $optional = false; if (0 === strpos($val, '[:')) { $optional = true; $val = substr($val, 1, -1); } if (0 === strpos($val, ':')) { $name = substr($val, 1); $type = $optional ? 2 : 1; $var[$name] = $type; } } return $var; } ~~~ ~~~ public static function clearAliasCache() { Cache::rm('think_route_alias'); } ~~~ ## 2 文件分析 1 `public static function build($url = '', $vars = '', $suffix = true, $domain = false){}` 生成Url > $url:url基础表达式 > $vars:url参数 > $suffix:伪静态后缀 > $domain:域名参数 2 `protected static function parseUrl($url){}` 解析url地址 > $url:待解析url参数 3 `protected static function parseDomain($url, $domain){}` 解析url地址,域名 > $url:待解析url参数 > $domain:待解析domain域名参数 4 `protected static function pattern($pattern, $vars){}` 解析路由规则中的变量是否有传入 > $pattern: 路由规则 > $vars: 规则参数 5 `protected static function parseSuffix($suffix){}` 解析url后缀 > $suffix:url后缀参数 6 `public static function isSsl(){}` 判断是否开启Ssl 7 `public static function getRouteUrl($alias, &$vars = []){}` 匹配路由地址 > $alias: 路由别名 > $vars: 路由规则参数 8 `private static function getRouteAlias(){}` 生成路由别名并缓存 9 `private static function parseVar($rule){}` 分析路由规则中的变量 10 `public static function clearAliasCache(){}` 清空路由别名缓存 ## 3 使用方法 TODO: ## 4 总 结 4个public方法 > build() > isSsl() > getRouteUrl() > clearAliasCache 4个protected方法 > parseUrl() > parseDomain() > pattern() > parseSuffix() 2个private方法 > getRouteAlias() > parseVar()