AliasRule.php 3.7 KB

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