Route.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Think;
  17. /**
  18. * ThinkPHP路由解析类
  19. */
  20. class Route {
  21. // 路由检测
  22. public static function check(){
  23. $depr = C('URL_PATHINFO_DEPR');
  24. $regx = preg_replace('/\.'.__EXT__.'$/i','',trim($_SERVER['PATH_INFO'],$depr));
  25. // 分隔符替换 确保路由定义使用统一的分隔符
  26. if('/' != $depr){
  27. $regx = str_replace($depr,'/',$regx);
  28. }
  29. // URL映射定义(静态路由)
  30. $maps = C('URL_MAP_RULES');
  31. if(isset($maps[$regx])) {
  32. $var = self::parseUrl($maps[$regx]);
  33. $_GET = array_merge($var, $_GET);
  34. return true;
  35. }
  36. // 动态路由处理
  37. $routes = C('URL_ROUTE_RULES');
  38. if(!empty($routes)) {
  39. foreach ($routes as $rule=>$route){
  40. if(is_numeric($rule)){
  41. // 支持 array('rule','adddress',...) 定义路由
  42. $rule = array_shift($route);
  43. }
  44. if(is_array($route) && isset($route[2])){
  45. // 路由参数
  46. $options = $route[2];
  47. if(isset($options['ext']) && __EXT__ != $options['ext']){
  48. // URL后缀检测
  49. continue;
  50. }
  51. if(isset($options['method']) && REQUEST_METHOD != strtoupper($options['method'])){
  52. // 请求类型检测
  53. continue;
  54. }
  55. // 自定义检测
  56. if(!empty($options['callback']) && is_callable($options['callback'])) {
  57. if(false === call_user_func($options['callback'])) {
  58. continue;
  59. }
  60. }
  61. }
  62. if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
  63. if($route instanceof \Closure) {
  64. // 执行闭包
  65. $result = self::invokeRegx($route, $matches);
  66. // 如果返回布尔值 则继续执行
  67. return is_bool($result) ? $result : exit;
  68. }else{
  69. return self::parseRegex($matches,$route,$regx);
  70. }
  71. }else{ // 规则路由
  72. $len1 = substr_count($regx,'/');
  73. $len2 = substr_count($rule,'/');
  74. if($len1>=$len2 || strpos($rule,'[')) {
  75. if('$' == substr($rule,-1,1)) {// 完整匹配
  76. if($len1 != $len2) {
  77. continue;
  78. }else{
  79. $rule = substr($rule,0,-1);
  80. }
  81. }
  82. $match = self::checkUrlMatch($regx,$rule);
  83. if(false !== $match) {
  84. if($route instanceof \Closure) {
  85. // 执行闭包
  86. $result = self::invokeRule($route, $match);
  87. // 如果返回布尔值 则继续执行
  88. return is_bool($result) ? $result : exit;
  89. }else{
  90. return self::parseRule($rule,$route,$regx);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99. // 检测URL和规则路由是否匹配
  100. private static function checkUrlMatch($regx,$rule) {
  101. $m1 = explode('/',$regx);
  102. $m2 = explode('/',$rule);
  103. $var = array();
  104. foreach ($m2 as $key=>$val){
  105. if(0 === strpos($val,'[:')){
  106. $val = substr($val,1,-1);
  107. }
  108. if(':' == substr($val,0,1)) {// 动态变量
  109. if($pos = strpos($val,'|')){
  110. // 使用函数过滤
  111. $val = substr($val,1,$pos-1);
  112. }
  113. if(strpos($val,'\\')) {
  114. $type = substr($val,-1);
  115. if('d'==$type) {
  116. if(isset($m1[$key]) && !is_numeric($m1[$key]))
  117. return false;
  118. }
  119. $name = substr($val, 1, -2);
  120. }elseif($pos = strpos($val,'^')){
  121. $array = explode('-',substr(strstr($val,'^'),1));
  122. if(in_array($m1[$key],$array)) {
  123. return false;
  124. }
  125. $name = substr($val, 1, $pos - 1);
  126. }else{
  127. $name = substr($val, 1);
  128. }
  129. $var[$name] = isset($m1[$key])?$m1[$key]:'';
  130. }elseif(0 !== strcasecmp($val,$m1[$key])){
  131. return false;
  132. }
  133. }
  134. // 成功匹配后返回URL中的动态变量数组
  135. return $var;
  136. }
  137. // 解析规范的路由地址
  138. // 地址格式 [控制器/操作?]参数1=值1&参数2=值2...
  139. private static function parseUrl($url) {
  140. $var = array();
  141. if(false !== strpos($url,'?')) { // [控制器/操作?]参数1=值1&参数2=值2...
  142. $info = parse_url($url);
  143. $path = explode('/',$info['path']);
  144. parse_str($info['query'],$var);
  145. }elseif(strpos($url,'/')){ // [控制器/操作]
  146. $path = explode('/',$url);
  147. }else{ // 参数1=值1&参数2=值2...
  148. parse_str($url,$var);
  149. }
  150. if(isset($path)) {
  151. $var[C('VAR_ACTION')] = array_pop($path);
  152. if(!empty($path)) {
  153. $var[C('VAR_CONTROLLER')] = array_pop($path);
  154. }
  155. if(!empty($path)) {
  156. $var[C('VAR_MODULE')] = array_pop($path);
  157. }
  158. }
  159. return $var;
  160. }
  161. // 解析规则路由
  162. // '路由规则'=>'[控制器/操作]?额外参数1=值1&额外参数2=值2...'
  163. // '路由规则'=>array('[控制器/操作]','额外参数1=值1&额外参数2=值2...')
  164. // '路由规则'=>'外部地址'
  165. // '路由规则'=>array('外部地址','重定向代码')
  166. // 路由规则中 :开头 表示动态变量
  167. // 外部地址中可以用动态变量 采用 :1 :2 的方式
  168. // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
  169. // 'new/:id'=>array('/new.php?id=:1',301), 重定向
  170. private static function parseRule($rule,$route,$regx) {
  171. // 获取路由地址规则
  172. $url = is_array($route)?$route[0]:$route;
  173. // 获取URL地址中的参数
  174. $paths = explode('/',$regx);
  175. // 解析路由规则
  176. $matches = array();
  177. $rule = explode('/',$rule);
  178. foreach ($rule as $item){
  179. $fun = '';
  180. if(0 === strpos($item,'[:')){
  181. $item = substr($item,1,-1);
  182. }
  183. if(0===strpos($item,':')) { // 动态变量获取
  184. if($pos = strpos($item,'|')){
  185. // 支持函数过滤
  186. $fun = substr($item,$pos+1);
  187. $item = substr($item,0,$pos);
  188. }
  189. if($pos = strpos($item,'^') ) {
  190. $var = substr($item,1,$pos-1);
  191. }elseif(strpos($item,'\\')){
  192. $var = substr($item,1,-2);
  193. }else{
  194. $var = substr($item,1);
  195. }
  196. $matches[$var] = !empty($fun)? $fun(array_shift($paths)) : array_shift($paths);
  197. }else{ // 过滤URL中的静态变量
  198. array_shift($paths);
  199. }
  200. }
  201. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
  202. if(strpos($url,':')) { // 传递动态参数
  203. $values = array_values($matches);
  204. $url = preg_replace_callback('/:(\d+)/', function($match) use($values){ return $values[$match[1] - 1]; }, $url);
  205. }
  206. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  207. exit;
  208. }else{
  209. // 解析路由地址
  210. $var = self::parseUrl($url);
  211. // 解析路由地址里面的动态参数
  212. $values = array_values($matches);
  213. foreach ($var as $key=>$val){
  214. if(0===strpos($val,':')) {
  215. $var[$key] = $values[substr($val,1)-1];
  216. }
  217. }
  218. $var = array_merge($matches,$var);
  219. // 解析剩余的URL参数
  220. if(!empty($paths)) {
  221. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
  222. }
  223. // 解析路由自动传入参数
  224. if(is_array($route) && isset($route[1])) {
  225. if(is_array($route[1])){
  226. $params = $route[1];
  227. }else{
  228. parse_str($route[1],$params);
  229. }
  230. $var = array_merge($var,$params);
  231. }
  232. $_GET = array_merge($var,$_GET);
  233. }
  234. return true;
  235. }
  236. // 解析正则路由
  237. // '路由正则'=>'[控制器/操作]?参数1=值1&参数2=值2...'
  238. // '路由正则'=>array('[控制器/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...')
  239. // '路由正则'=>'外部地址'
  240. // '路由正则'=>array('外部地址','重定向代码')
  241. // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
  242. // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
  243. // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
  244. private static function parseRegex($matches,$route,$regx) {
  245. // 获取路由地址规则
  246. $url = is_array($route)?$route[0]:$route;
  247. $url = preg_replace_callback('/:(\d+)/', function($match) use($matches){return $matches[$match[1]];}, $url);
  248. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
  249. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  250. exit;
  251. }else{
  252. // 解析路由地址
  253. $var = self::parseUrl($url);
  254. // 处理函数
  255. foreach($var as $key=>$val){
  256. if(strpos($val,'|')){
  257. list($val,$fun) = explode('|',$val);
  258. $var[$key] = $fun($val);
  259. }
  260. }
  261. // 解析剩余的URL参数
  262. $regx = substr_replace($regx,'',0,strlen($matches[0]));
  263. if($regx) {
  264. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
  265. $var[strtolower($match[1])] = strip_tags($match[2]);
  266. }, $regx);
  267. }
  268. // 解析路由自动传入参数
  269. if(is_array($route) && isset($route[1])) {
  270. if(is_array($route[1])){
  271. $params = $route[1];
  272. }else{
  273. parse_str($route[1],$params);
  274. }
  275. $var = array_merge($var,$params);
  276. }
  277. $_GET = array_merge($var,$_GET);
  278. }
  279. return true;
  280. }
  281. // 执行正则匹配下的闭包方法 支持参数调用
  282. static private function invokeRegx($closure, $var = array()) {
  283. $reflect = new \ReflectionFunction($closure);
  284. $params = $reflect->getParameters();
  285. $args = array();
  286. array_shift($var);
  287. foreach ($params as $param){
  288. if(!empty($var)) {
  289. $args[] = array_shift($var);
  290. }elseif($param->isDefaultValueAvailable()){
  291. $args[] = $param->getDefaultValue();
  292. }
  293. }
  294. return $reflect->invokeArgs($args);
  295. }
  296. // 执行规则匹配下的闭包方法 支持参数调用
  297. static private function invokeRule($closure, $var = array()) {
  298. $reflect = new \ReflectionFunction($closure);
  299. $params = $reflect->getParameters();
  300. $args = array();
  301. foreach ($params as $param){
  302. $name = $param->getName();
  303. if(isset($var[$name])) {
  304. $args[] = $var[$name];
  305. }elseif($param->isDefaultValueAvailable()){
  306. $args[] = $param->getDefaultValue();
  307. }
  308. }
  309. return $reflect->invokeArgs($args);
  310. }
  311. }