AliasRule.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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;
  12. use think\Route;
  13. class AliasRule extends Domain
  14. {
  15. /**
  16. * 架构函数
  17. * @access public
  18. * @param Route $router 路由实例
  19. * @param RuleGroup $parent 上级对象
  20. * @param string $name 路由别名
  21. * @param string $route 路由绑定
  22. * @param array $option 路由参数
  23. */
  24. public function __construct(Route $router, RuleGroup $parent, $name, $route, $option = [])
  25. {
  26. $this->router = $router;
  27. $this->parent = $parent;
  28. $this->name = $name;
  29. $this->route = $route;
  30. $this->option = $option;
  31. }
  32. /**
  33. * 检测路由别名
  34. * @access public
  35. * @param Request $request 请求对象
  36. * @param string $url 访问地址
  37. * @param bool $completeMatch 路由是否完全匹配
  38. * @return Dispatch|false
  39. */
  40. public function check($request, $url, $completeMatch = false)
  41. {
  42. if ($dispatch = $this->checkCrossDomain($request)) {
  43. // 允许跨域
  44. return $dispatch;
  45. }
  46. // 检查参数有效性
  47. if (!$this->checkOption($this->option, $request)) {
  48. return false;
  49. }
  50. list($action, $bind) = array_pad(explode('|', $url, 2), 2, '');
  51. if (isset($this->option['allow']) && !in_array($action, $this->option['allow'])) {
  52. // 允许操作
  53. return false;
  54. } elseif (isset($this->option['except']) && in_array($action, $this->option['except'])) {
  55. // 排除操作
  56. return false;
  57. }
  58. if (isset($this->option['method'][$action])) {
  59. $this->option['method'] = $this->option['method'][$action];
  60. }
  61. // 匹配后执行的行为
  62. $this->afterMatchGroup($request);
  63. if ($this->parent) {
  64. // 合并分组参数
  65. $this->mergeGroupOptions();
  66. }
  67. if (isset($this->option['ext'])) {
  68. // 路由ext参数 优先于系统配置的URL伪静态后缀参数
  69. $bind = preg_replace('/\.(' . $request->ext() . ')$/i', '', $bind);
  70. }
  71. $this->parseBindAppendParam($this->route);
  72. if (0 === strpos($this->route, '\\')) {
  73. // 路由到类
  74. return $this->bindToClass($request, $bind, substr($this->route, 1));
  75. } elseif (0 === strpos($this->route, '@')) {
  76. // 路由到控制器类
  77. return $this->bindToController($request, $bind, substr($this->route, 1));
  78. } else {
  79. // 路由到模块/控制器
  80. return $this->bindToModule($request, $bind, $this->route);
  81. }
  82. }
  83. /**
  84. * 设置允许的操作方法
  85. * @access public
  86. * @param array $action 操作方法
  87. * @return $this
  88. */
  89. public function allow($action = [])
  90. {
  91. return $this->option('allow', $action);
  92. }
  93. /**
  94. * 设置排除的操作方法
  95. * @access public
  96. * @param array $action 操作方法
  97. * @return $this
  98. */
  99. public function except($action = [])
  100. {
  101. return $this->option('except', $action);
  102. }
  103. }