Url.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. class Url
  13. {
  14. // 生成URL地址的root
  15. protected static $root;
  16. protected static $bindCheck;
  17. /**
  18. * URL生成 支持路由反射
  19. * @param string $url 路由地址
  20. * @param string|array $vars 参数(支持数组和字符串)a=val&b=val2... ['a'=>'val1', 'b'=>'val2']
  21. * @param string|bool $suffix 伪静态后缀,默认为true表示获取配置值
  22. * @param boolean|string $domain 是否显示域名 或者直接传入域名
  23. * @return string
  24. */
  25. public static function build($url = '', $vars = '', $suffix = true, $domain = false)
  26. {
  27. if (false === $domain && Route::rules('domain')) {
  28. $domain = true;
  29. }
  30. // 解析URL
  31. if (0 === strpos($url, '[') && $pos = strpos($url, ']')) {
  32. // [name] 表示使用路由命名标识生成URL
  33. $name = substr($url, 1, $pos - 1);
  34. $url = 'name' . substr($url, $pos + 1);
  35. }
  36. if (false === strpos($url, '://') && 0 !== strpos($url, '/')) {
  37. $info = parse_url($url);
  38. $url = !empty($info['path']) ? $info['path'] : '';
  39. if (isset($info['fragment'])) {
  40. // 解析锚点
  41. $anchor = $info['fragment'];
  42. if (false !== strpos($anchor, '?')) {
  43. // 解析参数
  44. list($anchor, $info['query']) = explode('?', $anchor, 2);
  45. }
  46. if (false !== strpos($anchor, '@')) {
  47. // 解析域名
  48. list($anchor, $domain) = explode('@', $anchor, 2);
  49. }
  50. } elseif (strpos($url, '@') && false === strpos($url, '\\')) {
  51. // 解析域名
  52. list($url, $domain) = explode('@', $url, 2);
  53. }
  54. }
  55. // 解析参数
  56. if (is_string($vars)) {
  57. // aaa=1&bbb=2 转换成数组
  58. parse_str($vars, $vars);
  59. }
  60. if ($url) {
  61. $rule = Route::name(isset($name) ? $name : $url . (isset($info['query']) ? '?' . $info['query'] : ''));
  62. if (is_null($rule) && isset($info['query'])) {
  63. $rule = Route::name($url);
  64. // 解析地址里面参数 合并到vars
  65. parse_str($info['query'], $params);
  66. $vars = array_merge($params, $vars);
  67. unset($info['query']);
  68. }
  69. }
  70. if (!empty($rule) && $match = self::getRuleUrl($rule, $vars)) {
  71. // 匹配路由命名标识
  72. $url = $match[0];
  73. // 替换可选分隔符
  74. $url = preg_replace(['/(\W)\?$/', '/(\W)\?/'], ['', '\1'], $url);
  75. if (!empty($match[1])) {
  76. $domain = $match[1];
  77. }
  78. if (!is_null($match[2])) {
  79. $suffix = $match[2];
  80. }
  81. } elseif (!empty($rule) && isset($name)) {
  82. throw new \InvalidArgumentException('route name not exists:' . $name);
  83. } else {
  84. // 检查别名路由
  85. $alias = Route::rules('alias');
  86. $matchAlias = false;
  87. if ($alias) {
  88. // 别名路由解析
  89. foreach ($alias as $key => $val) {
  90. if (is_array($val)) {
  91. $val = $val[0];
  92. }
  93. if (0 === strpos($url, $val)) {
  94. $url = $key . substr($url, strlen($val));
  95. $matchAlias = true;
  96. break;
  97. }
  98. }
  99. }
  100. if (!$matchAlias) {
  101. // 路由标识不存在 直接解析
  102. $url = self::parseUrl($url, $domain);
  103. }
  104. if (isset($info['query'])) {
  105. // 解析地址里面参数 合并到vars
  106. parse_str($info['query'], $params);
  107. $vars = array_merge($params, $vars);
  108. }
  109. }
  110. // 检测URL绑定
  111. if (!self::$bindCheck) {
  112. $type = Route::getBind('type');
  113. if ($type) {
  114. $bind = Route::getBind($type);
  115. if ($bind && 0 === strpos($url, $bind)) {
  116. $url = substr($url, strlen($bind) + 1);
  117. }
  118. }
  119. }
  120. // 还原URL分隔符
  121. $depr = Config::get('pathinfo_depr');
  122. $url = str_replace('/', $depr, $url);
  123. // URL后缀
  124. $suffix = in_array($url, ['/', '']) ? '' : self::parseSuffix($suffix);
  125. // 锚点
  126. $anchor = !empty($anchor) ? '#' . $anchor : '';
  127. // 参数组装
  128. if (!empty($vars)) {
  129. // 添加参数
  130. if (Config::get('url_common_param')) {
  131. $vars = http_build_query($vars);
  132. $url .= $suffix . '?' . $vars . $anchor;
  133. } else {
  134. $paramType = Config::get('url_param_type');
  135. foreach ($vars as $var => $val) {
  136. if ('' !== trim($val)) {
  137. if ($paramType) {
  138. $url .= $depr . urlencode($val);
  139. } else {
  140. $url .= $depr . $var . $depr . urlencode($val);
  141. }
  142. }
  143. }
  144. $url .= $suffix . $anchor;
  145. }
  146. } else {
  147. $url .= $suffix . $anchor;
  148. }
  149. // 检测域名
  150. $domain = self::parseDomain($url, $domain);
  151. // URL组装
  152. $url = $domain . rtrim(self::$root ?: Request::instance()->root(), '/') . '/' . ltrim($url, '/');
  153. self::$bindCheck = false;
  154. return $url;
  155. }
  156. // 直接解析URL地址
  157. protected static function parseUrl($url, &$domain)
  158. {
  159. $request = Request::instance();
  160. if (0 === strpos($url, '/')) {
  161. // 直接作为路由地址解析
  162. $url = substr($url, 1);
  163. } elseif (false !== strpos($url, '\\')) {
  164. // 解析到类
  165. $url = ltrim(str_replace('\\', '/', $url), '/');
  166. } elseif (0 === strpos($url, '@')) {
  167. // 解析到控制器
  168. $url = substr($url, 1);
  169. } else {
  170. // 解析到 模块/控制器/操作
  171. $module = $request->module();
  172. $domains = Route::rules('domain');
  173. if (true === $domain && 2 == substr_count($url, '/')) {
  174. $current = $request->host();
  175. $match = [];
  176. $pos = [];
  177. foreach ($domains as $key => $item) {
  178. if (isset($item['[bind]']) && 0 === strpos($url, $item['[bind]'][0])) {
  179. $pos[$key] = strlen($item['[bind]'][0]) + 1;
  180. $match[] = $key;
  181. $module = '';
  182. }
  183. }
  184. if ($match) {
  185. $domain = current($match);
  186. foreach ($match as $item) {
  187. if (0 === strpos($current, $item)) {
  188. $domain = $item;
  189. }
  190. }
  191. self::$bindCheck = true;
  192. $url = substr($url, $pos[$domain]);
  193. }
  194. } elseif ($domain) {
  195. if (isset($domains[$domain]['[bind]'][0])) {
  196. $bindModule = $domains[$domain]['[bind]'][0];
  197. if ($bindModule && !in_array($bindModule[0], ['\\', '@'])) {
  198. $module = '';
  199. }
  200. }
  201. }
  202. $module = $module ? $module . '/' : '';
  203. $controller = $request->controller();
  204. if ('' == $url) {
  205. // 空字符串输出当前的 模块/控制器/操作
  206. $action = $request->action();
  207. } else {
  208. $path = explode('/', $url);
  209. $action = array_pop($path);
  210. $controller = empty($path) ? $controller : array_pop($path);
  211. $module = empty($path) ? $module : array_pop($path) . '/';
  212. }
  213. if (Config::get('url_convert')) {
  214. $action = strtolower($action);
  215. $controller = Loader::parseName($controller);
  216. }
  217. $url = $module . $controller . '/' . $action;
  218. }
  219. return $url;
  220. }
  221. // 检测域名
  222. protected static function parseDomain(&$url, $domain)
  223. {
  224. if (!$domain) {
  225. return '';
  226. }
  227. $request = Request::instance();
  228. $rootDomain = Config::get('url_domain_root');
  229. if (true === $domain) {
  230. // 自动判断域名
  231. $domain = Config::get('app_host') ?: $request->host();
  232. $domains = Route::rules('domain');
  233. if ($domains) {
  234. $route_domain = array_keys($domains);
  235. foreach ($route_domain as $domain_prefix) {
  236. if (0 === strpos($domain_prefix, '*.') && strpos($domain, ltrim($domain_prefix, '*.')) !== false) {
  237. foreach ($domains as $key => $rule) {
  238. $rule = is_array($rule) ? $rule[0] : $rule;
  239. if (is_string($rule) && false === strpos($key, '*') && 0 === strpos($url, $rule)) {
  240. $url = ltrim($url, $rule);
  241. $domain = $key;
  242. // 生成对应子域名
  243. if (!empty($rootDomain)) {
  244. $domain .= $rootDomain;
  245. }
  246. break;
  247. } elseif (false !== strpos($key, '*')) {
  248. if (!empty($rootDomain)) {
  249. $domain .= $rootDomain;
  250. }
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. } else {
  258. if (empty($rootDomain)) {
  259. $host = Config::get('app_host') ?: $request->host();
  260. $rootDomain = substr_count($host, '.') > 1 ? substr(strstr($host, '.'), 1) : $host;
  261. }
  262. if (substr_count($domain, '.') < 2 && !strpos($domain, $rootDomain)) {
  263. $domain .= '.' . $rootDomain;
  264. }
  265. }
  266. if (false !== strpos($domain, '://')) {
  267. $scheme = '';
  268. } else {
  269. $scheme = $request->isSsl() || Config::get('is_https') ? 'https://' : 'http://';
  270. }
  271. return $scheme . $domain;
  272. }
  273. // 解析URL后缀
  274. protected static function parseSuffix($suffix)
  275. {
  276. if ($suffix) {
  277. $suffix = true === $suffix ? Config::get('url_html_suffix') : $suffix;
  278. if ($pos = strpos($suffix, '|')) {
  279. $suffix = substr($suffix, 0, $pos);
  280. }
  281. }
  282. return (empty($suffix) || 0 === strpos($suffix, '.')) ? $suffix : '.' . $suffix;
  283. }
  284. // 匹配路由地址
  285. public static function getRuleUrl($rule, &$vars = [])
  286. {
  287. foreach ($rule as $item) {
  288. list($url, $pattern, $domain, $suffix) = $item;
  289. if (empty($pattern)) {
  290. return [rtrim($url, '$'), $domain, $suffix];
  291. }
  292. $type = Config::get('url_common_param');
  293. foreach ($pattern as $key => $val) {
  294. if (isset($vars[$key])) {
  295. $url = str_replace(['[:' . $key . ']', '<' . $key . '?>', ':' . $key . '', '<' . $key . '>'], $type ? $vars[$key] : urlencode($vars[$key]), $url);
  296. unset($vars[$key]);
  297. $result = [$url, $domain, $suffix];
  298. } elseif (2 == $val) {
  299. $url = str_replace(['/[:' . $key . ']', '[:' . $key . ']', '<' . $key . '?>'], '', $url);
  300. $result = [$url, $domain, $suffix];
  301. } else {
  302. break;
  303. }
  304. }
  305. if (isset($result)) {
  306. return $result;
  307. }
  308. }
  309. return false;
  310. }
  311. // 指定当前生成URL地址的root
  312. public static function root($root)
  313. {
  314. self::$root = $root;
  315. Request::instance()->root($root);
  316. }
  317. }