Route.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. use think\exception\RouteNotFoundException;
  13. use think\route\AliasRule;
  14. use think\route\dispatch\Url as UrlDispatch;
  15. use think\route\Domain;
  16. use think\route\Resource;
  17. use think\route\RuleGroup;
  18. use think\route\RuleItem;
  19. class Route
  20. {
  21. /**
  22. * REST定义
  23. * @var array
  24. */
  25. protected $rest = [
  26. 'index' => ['get', '', 'index'],
  27. 'create' => ['get', '/create', 'create'],
  28. 'edit' => ['get', '/<id>/edit', 'edit'],
  29. 'read' => ['get', '/<id>', 'read'],
  30. 'save' => ['post', '', 'save'],
  31. 'update' => ['put', '/<id>', 'update'],
  32. 'delete' => ['delete', '/<id>', 'delete'],
  33. ];
  34. /**
  35. * 请求方法前缀定义
  36. * @var array
  37. */
  38. protected $methodPrefix = [
  39. 'get' => 'get',
  40. 'post' => 'post',
  41. 'put' => 'put',
  42. 'delete' => 'delete',
  43. 'patch' => 'patch',
  44. ];
  45. /**
  46. * 应用对象
  47. * @var App
  48. */
  49. protected $app;
  50. /**
  51. * 请求对象
  52. * @var Request
  53. */
  54. protected $request;
  55. /**
  56. * 当前HOST
  57. * @var string
  58. */
  59. protected $host;
  60. /**
  61. * 当前域名
  62. * @var string
  63. */
  64. protected $domain;
  65. /**
  66. * 当前分组对象
  67. * @var RuleGroup
  68. */
  69. protected $group;
  70. /**
  71. * 配置参数
  72. * @var array
  73. */
  74. protected $config = [];
  75. /**
  76. * 路由绑定
  77. * @var array
  78. */
  79. protected $bind = [];
  80. /**
  81. * 域名对象
  82. * @var array
  83. */
  84. protected $domains = [];
  85. /**
  86. * 跨域路由规则
  87. * @var RuleGroup
  88. */
  89. protected $cross;
  90. /**
  91. * 路由别名
  92. * @var array
  93. */
  94. protected $alias = [];
  95. /**
  96. * 路由是否延迟解析
  97. * @var bool
  98. */
  99. protected $lazy = true;
  100. /**
  101. * 路由是否测试模式
  102. * @var bool
  103. */
  104. protected $isTest;
  105. /**
  106. * (分组)路由规则是否合并解析
  107. * @var bool
  108. */
  109. protected $mergeRuleRegex = true;
  110. /**
  111. * 路由解析自动搜索多级控制器
  112. * @var bool
  113. */
  114. protected $autoSearchController = true;
  115. public function __construct(App $app, array $config = [])
  116. {
  117. $this->app = $app;
  118. $this->request = $app['request'];
  119. $this->config = $config;
  120. $this->host = $this->request->host(true) ?: $config['app_host'];
  121. $this->setDefaultDomain();
  122. }
  123. public function config($name = null)
  124. {
  125. if (is_null($name)) {
  126. return $this->config;
  127. }
  128. return isset($this->config[$name]) ? $this->config[$name] : null;
  129. }
  130. /**
  131. * 配置
  132. * @access public
  133. * @param array $config
  134. * @return void
  135. */
  136. public function setConfig(array $config = [])
  137. {
  138. $this->config = array_merge($this->config, array_change_key_case($config));
  139. }
  140. public static function __make(App $app, Config $config)
  141. {
  142. $config = $config->pull('app');
  143. $route = new static($app, $config);
  144. $route->lazy($config['url_lazy_route'])
  145. ->autoSearchController($config['controller_auto_search'])
  146. ->mergeRuleRegex($config['route_rule_merge']);
  147. return $route;
  148. }
  149. /**
  150. * 设置路由的请求对象实例
  151. * @access public
  152. * @param Request $request 请求对象实例
  153. * @return void
  154. */
  155. public function setRequest($request)
  156. {
  157. $this->request = $request;
  158. }
  159. /**
  160. * 设置路由域名及分组(包括资源路由)是否延迟解析
  161. * @access public
  162. * @param bool $lazy 路由是否延迟解析
  163. * @return $this
  164. */
  165. public function lazy($lazy = true)
  166. {
  167. $this->lazy = $lazy;
  168. return $this;
  169. }
  170. /**
  171. * 设置路由为测试模式
  172. * @access public
  173. * @param bool $test 路由是否测试模式
  174. * @return void
  175. */
  176. public function setTestMode($test)
  177. {
  178. $this->isTest = $test;
  179. }
  180. /**
  181. * 检查路由是否为测试模式
  182. * @access public
  183. * @return bool
  184. */
  185. public function isTest()
  186. {
  187. return $this->isTest;
  188. }
  189. /**
  190. * 设置路由域名及分组(包括资源路由)是否合并解析
  191. * @access public
  192. * @param bool $merge 路由是否合并解析
  193. * @return $this
  194. */
  195. public function mergeRuleRegex($merge = true)
  196. {
  197. $this->mergeRuleRegex = $merge;
  198. $this->group->mergeRuleRegex($merge);
  199. return $this;
  200. }
  201. /**
  202. * 设置路由自动解析是否搜索多级控制器
  203. * @access public
  204. * @param bool $auto 是否自动搜索多级控制器
  205. * @return $this
  206. */
  207. public function autoSearchController($auto = true)
  208. {
  209. $this->autoSearchController = $auto;
  210. return $this;
  211. }
  212. /**
  213. * 初始化默认域名
  214. * @access protected
  215. * @return void
  216. */
  217. protected function setDefaultDomain()
  218. {
  219. // 默认域名
  220. $this->domain = $this->host;
  221. // 注册默认域名
  222. $domain = new Domain($this, $this->host);
  223. $this->domains[$this->host] = $domain;
  224. // 默认分组
  225. $this->group = $domain;
  226. }
  227. /**
  228. * 设置当前域名
  229. * @access public
  230. * @param RuleGroup $group 域名
  231. * @return void
  232. */
  233. public function setGroup(RuleGroup $group)
  234. {
  235. $this->group = $group;
  236. }
  237. /**
  238. * 获取当前分组
  239. * @access public
  240. * @return RuleGroup
  241. */
  242. public function getGroup()
  243. {
  244. return $this->group;
  245. }
  246. /**
  247. * 注册变量规则
  248. * @access public
  249. * @param string|array $name 变量名
  250. * @param string $rule 变量规则
  251. * @return $this
  252. */
  253. public function pattern($name, $rule = '')
  254. {
  255. $this->group->pattern($name, $rule);
  256. return $this;
  257. }
  258. /**
  259. * 注册路由参数
  260. * @access public
  261. * @param string|array $name 参数名
  262. * @param mixed $value 值
  263. * @return $this
  264. */
  265. public function option($name, $value = '')
  266. {
  267. $this->group->option($name, $value);
  268. return $this;
  269. }
  270. /**
  271. * 注册域名路由
  272. * @access public
  273. * @param string|array $name 子域名
  274. * @param mixed $rule 路由规则
  275. * @param array $option 路由参数
  276. * @param array $pattern 变量规则
  277. * @return Domain
  278. */
  279. public function domain($name, $rule = '', $option = [], $pattern = [])
  280. {
  281. // 支持多个域名使用相同路由规则
  282. $domainName = is_array($name) ? array_shift($name) : $name;
  283. if ('*' != $domainName && false === strpos($domainName, '.')) {
  284. $domainName .= '.' . $this->request->rootDomain();
  285. }
  286. if (!isset($this->domains[$domainName])) {
  287. $domain = (new Domain($this, $domainName, $rule, $option, $pattern))
  288. ->lazy($this->lazy)
  289. ->mergeRuleRegex($this->mergeRuleRegex);
  290. $this->domains[$domainName] = $domain;
  291. } else {
  292. $domain = $this->domains[$domainName];
  293. $domain->parseGroupRule($rule);
  294. }
  295. if (is_array($name) && !empty($name)) {
  296. $root = $this->request->rootDomain();
  297. foreach ($name as $item) {
  298. if (false === strpos($item, '.')) {
  299. $item .= '.' . $root;
  300. }
  301. $this->domains[$item] = $domainName;
  302. }
  303. }
  304. // 返回域名对象
  305. return $domain;
  306. }
  307. /**
  308. * 获取域名
  309. * @access public
  310. * @return array
  311. */
  312. public function getDomains()
  313. {
  314. return $this->domains;
  315. }
  316. /**
  317. * 设置路由绑定
  318. * @access public
  319. * @param string $bind 绑定信息
  320. * @param string $domain 域名
  321. * @return $this
  322. */
  323. public function bind($bind, $domain = null)
  324. {
  325. $domain = is_null($domain) ? $this->domain : $domain;
  326. $this->bind[$domain] = $bind;
  327. return $this;
  328. }
  329. /**
  330. * 读取路由绑定
  331. * @access public
  332. * @param string $domain 域名
  333. * @return string|null
  334. */
  335. public function getBind($domain = null)
  336. {
  337. if (is_null($domain)) {
  338. $domain = $this->domain;
  339. } elseif (true === $domain) {
  340. return $this->bind;
  341. } elseif (false === strpos($domain, '.')) {
  342. $domain .= '.' . $this->request->rootDomain();
  343. }
  344. $subDomain = $this->request->subDomain();
  345. if (strpos($subDomain, '.')) {
  346. $name = '*' . strstr($subDomain, '.');
  347. }
  348. if (isset($this->bind[$domain])) {
  349. $result = $this->bind[$domain];
  350. } elseif (isset($name) && isset($this->bind[$name])) {
  351. $result = $this->bind[$name];
  352. } elseif (!empty($subDomain) && isset($this->bind['*'])) {
  353. $result = $this->bind['*'];
  354. } else {
  355. $result = null;
  356. }
  357. return $result;
  358. }
  359. /**
  360. * 读取路由标识
  361. * @access public
  362. * @param string $name 路由标识
  363. * @param string $domain 域名
  364. * @return mixed
  365. */
  366. public function getName($name = null, $domain = null, $method = '*')
  367. {
  368. return $this->app['rule_name']->get($name, $domain, $method);
  369. }
  370. /**
  371. * 读取路由
  372. * @access public
  373. * @param string $rule 路由规则
  374. * @param string $domain 域名
  375. * @return array
  376. */
  377. public function getRule($rule, $domain = null)
  378. {
  379. if (is_null($domain)) {
  380. $domain = $this->domain;
  381. }
  382. return $this->app['rule_name']->getRule($rule, $domain);
  383. }
  384. /**
  385. * 读取路由
  386. * @access public
  387. * @param string $domain 域名
  388. * @return array
  389. */
  390. public function getRuleList($domain = null)
  391. {
  392. return $this->app['rule_name']->getRuleList($domain);
  393. }
  394. /**
  395. * 批量导入路由标识
  396. * @access public
  397. * @param array $name 路由标识
  398. * @return $this
  399. */
  400. public function setName($name)
  401. {
  402. $this->app['rule_name']->import($name);
  403. return $this;
  404. }
  405. /**
  406. * 导入配置文件的路由规则
  407. * @access public
  408. * @param array $rules 路由规则
  409. * @param string $type 请求类型
  410. * @return void
  411. */
  412. public function import(array $rules, $type = '*')
  413. {
  414. // 检查域名部署
  415. if (isset($rules['__domain__'])) {
  416. foreach ($rules['__domain__'] as $key => $rule) {
  417. $this->domain($key, $rule);
  418. }
  419. unset($rules['__domain__']);
  420. }
  421. // 检查变量规则
  422. if (isset($rules['__pattern__'])) {
  423. $this->pattern($rules['__pattern__']);
  424. unset($rules['__pattern__']);
  425. }
  426. // 检查路由别名
  427. if (isset($rules['__alias__'])) {
  428. foreach ($rules['__alias__'] as $key => $val) {
  429. $this->alias($key, $val);
  430. }
  431. unset($rules['__alias__']);
  432. }
  433. // 检查资源路由
  434. if (isset($rules['__rest__'])) {
  435. foreach ($rules['__rest__'] as $key => $rule) {
  436. $this->resource($key, $rule);
  437. }
  438. unset($rules['__rest__']);
  439. }
  440. // 检查路由规则(包含分组)
  441. foreach ($rules as $key => $val) {
  442. if (is_numeric($key)) {
  443. $key = array_shift($val);
  444. }
  445. if (empty($val)) {
  446. continue;
  447. }
  448. if (is_string($key) && 0 === strpos($key, '[')) {
  449. $key = substr($key, 1, -1);
  450. $this->group($key, $val);
  451. } elseif (is_array($val)) {
  452. $this->rule($key, $val[0], $type, $val[1], isset($val[2]) ? $val[2] : []);
  453. } else {
  454. $this->rule($key, $val, $type);
  455. }
  456. }
  457. }
  458. /**
  459. * 注册路由规则
  460. * @access public
  461. * @param string $rule 路由规则
  462. * @param mixed $route 路由地址
  463. * @param string $method 请求类型
  464. * @param array $option 路由参数
  465. * @param array $pattern 变量规则
  466. * @return RuleItem
  467. */
  468. public function rule($rule, $route, $method = '*', array $option = [], array $pattern = [])
  469. {
  470. return $this->group->addRule($rule, $route, $method, $option, $pattern);
  471. }
  472. /**
  473. * 设置跨域有效路由规则
  474. * @access public
  475. * @param Rule $rule 路由规则
  476. * @param string $method 请求类型
  477. * @return $this
  478. */
  479. public function setCrossDomainRule($rule, $method = '*')
  480. {
  481. if (!isset($this->cross)) {
  482. $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
  483. }
  484. $this->cross->addRuleItem($rule, $method);
  485. return $this;
  486. }
  487. /**
  488. * 批量注册路由规则
  489. * @access public
  490. * @param array $rules 路由规则
  491. * @param string $method 请求类型
  492. * @param array $option 路由参数
  493. * @param array $pattern 变量规则
  494. * @return void
  495. */
  496. public function rules($rules, $method = '*', array $option = [], array $pattern = [])
  497. {
  498. $this->group->addRules($rules, $method, $option, $pattern);
  499. }
  500. /**
  501. * 注册路由分组
  502. * @access public
  503. * @param string|array $name 分组名称或者参数
  504. * @param array|\Closure $route 分组路由
  505. * @param array $option 路由参数
  506. * @param array $pattern 变量规则
  507. * @return RuleGroup
  508. */
  509. public function group($name, $route, array $option = [], array $pattern = [])
  510. {
  511. if (is_array($name)) {
  512. $option = $name;
  513. $name = isset($option['name']) ? $option['name'] : '';
  514. }
  515. return (new RuleGroup($this, $this->group, $name, $route, $option, $pattern))
  516. ->lazy($this->lazy)
  517. ->mergeRuleRegex($this->mergeRuleRegex);
  518. }
  519. /**
  520. * 注册路由
  521. * @access public
  522. * @param string $rule 路由规则
  523. * @param mixed $route 路由地址
  524. * @param array $option 路由参数
  525. * @param array $pattern 变量规则
  526. * @return RuleItem
  527. */
  528. public function any($rule, $route = '', array $option = [], array $pattern = [])
  529. {
  530. return $this->rule($rule, $route, '*', $option, $pattern);
  531. }
  532. /**
  533. * 注册GET路由
  534. * @access public
  535. * @param string $rule 路由规则
  536. * @param mixed $route 路由地址
  537. * @param array $option 路由参数
  538. * @param array $pattern 变量规则
  539. * @return RuleItem
  540. */
  541. public function get($rule, $route = '', array $option = [], array $pattern = [])
  542. {
  543. return $this->rule($rule, $route, 'GET', $option, $pattern);
  544. }
  545. /**
  546. * 注册POST路由
  547. * @access public
  548. * @param string $rule 路由规则
  549. * @param mixed $route 路由地址
  550. * @param array $option 路由参数
  551. * @param array $pattern 变量规则
  552. * @return RuleItem
  553. */
  554. public function post($rule, $route = '', array $option = [], array $pattern = [])
  555. {
  556. return $this->rule($rule, $route, 'POST', $option, $pattern);
  557. }
  558. /**
  559. * 注册PUT路由
  560. * @access public
  561. * @param string $rule 路由规则
  562. * @param mixed $route 路由地址
  563. * @param array $option 路由参数
  564. * @param array $pattern 变量规则
  565. * @return RuleItem
  566. */
  567. public function put($rule, $route = '', array $option = [], array $pattern = [])
  568. {
  569. return $this->rule($rule, $route, 'PUT', $option, $pattern);
  570. }
  571. /**
  572. * 注册DELETE路由
  573. * @access public
  574. * @param string $rule 路由规则
  575. * @param mixed $route 路由地址
  576. * @param array $option 路由参数
  577. * @param array $pattern 变量规则
  578. * @return RuleItem
  579. */
  580. public function delete($rule, $route = '', array $option = [], array $pattern = [])
  581. {
  582. return $this->rule($rule, $route, 'DELETE', $option, $pattern);
  583. }
  584. /**
  585. * 注册PATCH路由
  586. * @access public
  587. * @param string $rule 路由规则
  588. * @param mixed $route 路由地址
  589. * @param array $option 路由参数
  590. * @param array $pattern 变量规则
  591. * @return RuleItem
  592. */
  593. public function patch($rule, $route = '', array $option = [], array $pattern = [])
  594. {
  595. return $this->rule($rule, $route, 'PATCH', $option, $pattern);
  596. }
  597. /**
  598. * 注册资源路由
  599. * @access public
  600. * @param string $rule 路由规则
  601. * @param string $route 路由地址
  602. * @param array $option 路由参数
  603. * @param array $pattern 变量规则
  604. * @return Resource
  605. */
  606. public function resource($rule, $route = '', array $option = [], array $pattern = [])
  607. {
  608. return (new Resource($this, $this->group, $rule, $route, $option, $pattern, $this->rest))
  609. ->lazy($this->lazy);
  610. }
  611. /**
  612. * 注册控制器路由 操作方法对应不同的请求前缀
  613. * @access public
  614. * @param string $rule 路由规则
  615. * @param string $route 路由地址
  616. * @param array $option 路由参数
  617. * @param array $pattern 变量规则
  618. * @return RuleGroup
  619. */
  620. public function controller($rule, $route = '', array $option = [], array $pattern = [])
  621. {
  622. $group = new RuleGroup($this, $this->group, $rule, null, $option, $pattern);
  623. foreach ($this->methodPrefix as $type => $val) {
  624. $group->addRule('<action>', $val . '<action>', $type);
  625. }
  626. return $group->prefix($route . '/');
  627. }
  628. /**
  629. * 注册视图路由
  630. * @access public
  631. * @param string|array $rule 路由规则
  632. * @param string $template 路由模板地址
  633. * @param array $vars 模板变量
  634. * @param array $option 路由参数
  635. * @param array $pattern 变量规则
  636. * @return RuleItem
  637. */
  638. public function view($rule, $template = '', array $vars = [], array $option = [], array $pattern = [])
  639. {
  640. return $this->rule($rule, $template, 'GET', $option, $pattern)->view($vars);
  641. }
  642. /**
  643. * 注册重定向路由
  644. * @access public
  645. * @param string|array $rule 路由规则
  646. * @param string $route 路由地址
  647. * @param array $status 状态码
  648. * @param array $option 路由参数
  649. * @param array $pattern 变量规则
  650. * @return RuleItem
  651. */
  652. public function redirect($rule, $route = '', $status = 301, array $option = [], array $pattern = [])
  653. {
  654. return $this->rule($rule, $route, '*', $option, $pattern)->redirect()->status($status);
  655. }
  656. /**
  657. * 注册别名路由
  658. * @access public
  659. * @param string $rule 路由别名
  660. * @param string $route 路由地址
  661. * @param array $option 路由参数
  662. * @return AliasRule
  663. */
  664. public function alias($rule, $route, array $option = [])
  665. {
  666. $aliasRule = new AliasRule($this, $this->group, $rule, $route, $option);
  667. $this->alias[$rule] = $aliasRule;
  668. return $aliasRule;
  669. }
  670. /**
  671. * 获取别名路由定义
  672. * @access public
  673. * @param string $name 路由别名
  674. * @return string|array|null
  675. */
  676. public function getAlias($name = null)
  677. {
  678. if (is_null($name)) {
  679. return $this->alias;
  680. }
  681. return isset($this->alias[$name]) ? $this->alias[$name] : null;
  682. }
  683. /**
  684. * 设置不同请求类型下面的方法前缀
  685. * @access public
  686. * @param string|array $method 请求类型
  687. * @param string $prefix 类型前缀
  688. * @return $this
  689. */
  690. public function setMethodPrefix($method, $prefix = '')
  691. {
  692. if (is_array($method)) {
  693. $this->methodPrefix = array_merge($this->methodPrefix, array_change_key_case($method));
  694. } else {
  695. $this->methodPrefix[strtolower($method)] = $prefix;
  696. }
  697. return $this;
  698. }
  699. /**
  700. * 获取请求类型的方法前缀
  701. * @access public
  702. * @param string $method 请求类型
  703. * @param string $prefix 类型前缀
  704. * @return string|null
  705. */
  706. public function getMethodPrefix($method)
  707. {
  708. $method = strtolower($method);
  709. return isset($this->methodPrefix[$method]) ? $this->methodPrefix[$method] : null;
  710. }
  711. /**
  712. * rest方法定义和修改
  713. * @access public
  714. * @param string $name 方法名称
  715. * @param array|bool $resource 资源
  716. * @return $this
  717. */
  718. public function rest($name, $resource = [])
  719. {
  720. if (is_array($name)) {
  721. $this->rest = $resource ? $name : array_merge($this->rest, $name);
  722. } else {
  723. $this->rest[$name] = $resource;
  724. }
  725. return $this;
  726. }
  727. /**
  728. * 获取rest方法定义的参数
  729. * @access public
  730. * @param string $name 方法名称
  731. * @return array|null
  732. */
  733. public function getRest($name = null)
  734. {
  735. if (is_null($name)) {
  736. return $this->rest;
  737. }
  738. return isset($this->rest[$name]) ? $this->rest[$name] : null;
  739. }
  740. /**
  741. * 注册未匹配路由规则后的处理
  742. * @access public
  743. * @param string $route 路由地址
  744. * @param string $method 请求类型
  745. * @param array $option 路由参数
  746. * @return RuleItem
  747. */
  748. public function miss($route, $method = '*', array $option = [])
  749. {
  750. return $this->group->addMissRule($route, $method, $option);
  751. }
  752. /**
  753. * 注册一个自动解析的URL路由
  754. * @access public
  755. * @param string $route 路由地址
  756. * @return RuleItem
  757. */
  758. public function auto($route)
  759. {
  760. return $this->group->addAutoRule($route);
  761. }
  762. /**
  763. * 检测URL路由
  764. * @access public
  765. * @param string $url URL地址
  766. * @param bool $must 是否强制路由
  767. * @return Dispatch
  768. * @throws RouteNotFoundException
  769. */
  770. public function check($url, $must = false)
  771. {
  772. // 自动检测域名路由
  773. $domain = $this->checkDomain();
  774. $url = str_replace($this->config['pathinfo_depr'], '|', $url);
  775. $completeMatch = $this->config['route_complete_match'];
  776. $result = $domain->check($this->request, $url, $completeMatch);
  777. if (false === $result && !empty($this->cross)) {
  778. // 检测跨域路由
  779. $result = $this->cross->check($this->request, $url, $completeMatch);
  780. }
  781. if (false !== $result) {
  782. // 路由匹配
  783. return $result;
  784. } elseif ($must) {
  785. // 强制路由不匹配则抛出异常
  786. throw new RouteNotFoundException();
  787. }
  788. // 默认路由解析
  789. return new UrlDispatch($this->request, $this->group, $url, [
  790. 'auto_search' => $this->autoSearchController,
  791. ]);
  792. }
  793. /**
  794. * 检测域名的路由规则
  795. * @access protected
  796. * @return Domain
  797. */
  798. protected function checkDomain()
  799. {
  800. // 获取当前子域名
  801. $subDomain = $this->request->subDomain();
  802. $item = false;
  803. if ($subDomain && count($this->domains) > 1) {
  804. $domain = explode('.', $subDomain);
  805. $domain2 = array_pop($domain);
  806. if ($domain) {
  807. // 存在三级域名
  808. $domain3 = array_pop($domain);
  809. }
  810. if ($subDomain && isset($this->domains[$subDomain])) {
  811. // 子域名配置
  812. $item = $this->domains[$subDomain];
  813. } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
  814. // 泛三级域名
  815. $item = $this->domains['*.' . $domain2];
  816. $panDomain = $domain3;
  817. } elseif (isset($this->domains['*']) && !empty($domain2)) {
  818. // 泛二级域名
  819. if ('www' != $domain2) {
  820. $item = $this->domains['*'];
  821. $panDomain = $domain2;
  822. }
  823. }
  824. if (isset($panDomain)) {
  825. // 保存当前泛域名
  826. $this->request->setPanDomain($panDomain);
  827. }
  828. }
  829. if (false === $item) {
  830. // 检测当前完整域名
  831. $item = $this->domains[$this->host];
  832. }
  833. if (is_string($item)) {
  834. $item = $this->domains[$item];
  835. }
  836. return $item;
  837. }
  838. /**
  839. * 清空路由规则
  840. * @access public
  841. * @return void
  842. */
  843. public function clear()
  844. {
  845. $this->app['rule_name']->clear();
  846. $this->group->clear();
  847. }
  848. /**
  849. * 设置全局的路由分组参数
  850. * @access public
  851. * @param string $method 方法名
  852. * @param array $args 调用参数
  853. * @return RuleGroup
  854. */
  855. public function __call($method, $args)
  856. {
  857. return call_user_func_array([$this->group, $method], $args);
  858. }
  859. public function __debugInfo()
  860. {
  861. $data = get_object_vars($this);
  862. unset($data['app'], $data['request']);
  863. return $data;
  864. }
  865. }