Route.php 22 KB

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