Url.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\route\dispatch;
  12. use think\exception\HttpException;
  13. use think\Loader;
  14. use think\route\Dispatch;
  15. class Url extends Dispatch
  16. {
  17. public function init()
  18. {
  19. // 解析默认的URL规则
  20. $result = $this->parseUrl($this->dispatch);
  21. return (new Module($this->request, $this->rule, $result))->init();
  22. }
  23. public function exec()
  24. {}
  25. /**
  26. * 解析URL地址
  27. * @access protected
  28. * @param string $url URL
  29. * @return array
  30. */
  31. protected function parseUrl($url)
  32. {
  33. $depr = $this->rule->getConfig('pathinfo_depr');
  34. $bind = $this->rule->getRouter()->getBind();
  35. if (!empty($bind) && preg_match('/^[a-z]/is', $bind)) {
  36. $bind = str_replace('/', $depr, $bind);
  37. // 如果有模块/控制器绑定
  38. $url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr);
  39. }
  40. list($path, $var) = $this->rule->parseUrlPath($url);
  41. if (empty($path)) {
  42. return [null, null, null];
  43. }
  44. // 解析模块
  45. $module = $this->rule->getConfig('app_multi_module') ? array_shift($path) : null;
  46. if ($this->param['auto_search']) {
  47. $controller = $this->autoFindController($module, $path);
  48. } else {
  49. // 解析控制器
  50. $controller = !empty($path) ? array_shift($path) : null;
  51. }
  52. if ($controller && !preg_match('/^[A-Za-z0-9][\w|\.]*$/', $controller)) {
  53. throw new HttpException(404, 'controller not exists:' . $controller);
  54. }
  55. // 解析操作
  56. $action = !empty($path) ? array_shift($path) : null;
  57. // 解析额外参数
  58. if ($path) {
  59. if ($this->rule->getConfig('url_param_type')) {
  60. $var += $path;
  61. } else {
  62. preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
  63. $var[$match[1]] = strip_tags($match[2]);
  64. }, implode('|', $path));
  65. }
  66. }
  67. $panDomain = $this->request->panDomain();
  68. if ($panDomain && $key = array_search('*', $var)) {
  69. // 泛域名赋值
  70. $var[$key] = $panDomain;
  71. }
  72. // 设置当前请求的参数
  73. $this->request->setRouteVars($var);
  74. // 封装路由
  75. $route = [$module, $controller, $action];
  76. if ($this->hasDefinedRoute($route, $bind)) {
  77. throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url));
  78. }
  79. return $route;
  80. }
  81. /**
  82. * 检查URL是否已经定义过路由
  83. * @access protected
  84. * @param string $route 路由信息
  85. * @param string $bind 绑定信息
  86. * @return bool
  87. */
  88. protected function hasDefinedRoute($route, $bind)
  89. {
  90. list($module, $controller, $action) = $route;
  91. // 检查地址是否被定义过路由
  92. $name = strtolower($module . '/' . Loader::parseName($controller, 1) . '/' . $action);
  93. $name2 = '';
  94. if (empty($module) || $module == $bind) {
  95. $name2 = strtolower(Loader::parseName($controller, 1) . '/' . $action);
  96. }
  97. $host = $this->request->host(true);
  98. $method = $this->request->method();
  99. if ($this->rule->getRouter()->getName($name, $host, $method) || $this->rule->getRouter()->getName($name2, $host, $method)) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * 自动定位控制器类
  106. * @access protected
  107. * @param string $module 模块名
  108. * @param array $path URL
  109. * @return string
  110. */
  111. protected function autoFindController($module, &$path)
  112. {
  113. $dir = $this->app->getAppPath() . ($module ? $module . '/' : '') . $this->rule->getConfig('url_controller_layer');
  114. $suffix = $this->app->getSuffix() || $this->rule->getConfig('controller_suffix') ? ucfirst($this->rule->getConfig('url_controller_layer')) : '';
  115. $item = [];
  116. $find = false;
  117. foreach ($path as $val) {
  118. $item[] = $val;
  119. $file = $dir . '/' . str_replace('.', '/', $val) . $suffix . '.php';
  120. $file = pathinfo($file, PATHINFO_DIRNAME) . '/' . Loader::parseName(pathinfo($file, PATHINFO_FILENAME), 1) . '.php';
  121. if (is_file($file)) {
  122. $find = true;
  123. break;
  124. } else {
  125. $dir .= '/' . Loader::parseName($val);
  126. }
  127. }
  128. if ($find) {
  129. $controller = implode('.', $item);
  130. $path = array_slice($path, count($item));
  131. } else {
  132. $controller = array_shift($path);
  133. }
  134. return $controller;
  135. }
  136. }