切割类

9、\Think\Route->check() 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function check(): Dispatch
{
    //转换PATH_INFO分隔符,拼接url
    $url str_replace($this->config['pathinfo_depr'], '|'$this->path());
    //获取是否完全匹配 配置项
    $completeMatch $this->config['route_complete_match'];
    //调用checkDomain检测是否为域名路由如果不是则返回false
    //如果是域名路由,则返回一个Domain对象。并且执行对象中的check方法
    //并把当前的Request请求对象以及url地址和是否完全匹配路由项传入进去
    $result $this->checkDomain()->check($this->request, $url$completeMatch);
    //判断result是否为false 也就是不是域名路由
    //再判断是否为跨域路由
    if (false === $result && !empty($this->cross)) {
        // 如果是跨域路由,就将当前的Request请求对象以及url地址和是否完全匹配路由项传入进去
        $result $this->cross->check($this->request, $url$completeMatch);
    }
    //如果是域名路由
    if (false !== $result) {
        //直接返回 $result变量 变量内存储着 RuleGroup对象实例
        //路由规则
        return $result;
    elseif ($this->config['url_route_must']) {
        //判断是否启用了强制路由,如果启用了强制路由
        //然后域名路由也匹配不上。就触发一个路由
        //找不到的异常类
        throw new RouteNotFoundException();
    }
    //以上都不匹配,则调用url函数,传入当前的url地址
    //返回一个Url类实例
    return $this->url($url);
}

10、\Think\Route->url()

1
2
3
4
public function url(string $url): UrlDispatch
{
    return new UrlDispatch($this->request, $this->group, $url);
}

11、\Think\Route\dispatch\Url  url切割类(自己给的称呼)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//构造函数
public function __construct(Request $request, Rule $rule$dispatcharray $param = [], int $code = null)
{
    //获取传入来的Request对象,存储到类成员变量内
    $this->request = $request;
    //获取到路由列表,也放到类成员变量内
    $this->rule    = $rule;
    // 调用类中的parseUrl方法 解析URL规则
    $dispatch $this->parseUrl($dispatch);
    //调用父类构造函数
    parent::__construct($request$rule$dispatch$this->param, $code);
}
 
protected function parseUrl(string $url): array
{
    //获取到分隔符
    $depr $this->rule->config('pathinfo_depr');
    //获取当前域名
    $bind $this->rule->getRouter()->getDomainBind();
    //如果域名不为空,并且正则匹配的到
    if ($bind && preg_match('/^[a-z]/is'$bind)) {
        //切割url,换成配置项中的PATH_INFO分隔符
        $bind str_replace('/'$depr$bind);
        // 如果有域名绑定
        $url $bind . ('.' != substr($bind, -1) ? $depr '') . ltrim($url$depr);
    }
    //调用rule类中的parseUrlPath方法,切割pathinfo参数
    //如果url中有参数 返回一个demo吧  ['Index','Demo']
    //第一个为控制器、第二个为方法
    $path $this->rule->parseUrlPath($url);
    //如果切割的pathinfo为空,则直接返回一个[null,null] 这样的一个空数组
    if (empty($path)) {
        return [null, null];
    }
 
    //获取到第一个下标  控制器
    $controller = !empty($path) ? array_shift($path) : null;
    //正则匹配,如果匹配不到。就弹出一个HttpException异常
    if ($controller && !preg_match('/^[A-Za-z0-9][\w|\.]*$/'$controller)) {
        throw new HttpException(404, 'controller not exists:' $controller);
    }
    //获取到第二个下标  方法 function
    // 解析操作
    $action = !empty($path) ? array_shift($path) : null;
    $var    = [];
 
    // 解析额外参数
    //类似于  /index.php/Index/Users/Pascc
    //这样就会返回一个 三个下标的数组
    if ($path) {
        //这里将多余的下标,放到var变量内
        preg_replace_callback('/(\w+)\|([^\|]+)/'function ($matchuse (&$var) {
            $var[$match[1]] = strip_tags($match[2]);
        }, implode('|'$path));
    }
    //获取到泛域名 再判断其中是否有*符号
    $panDomain $this->request->panDomain();
    if ($panDomain && $key array_search('*'$var)) {
        // 泛域名赋值
        $var[$key] = $panDomain;
    }
 
    // 设置当前请求的参数
    $this->param = $var;
 
    // 封装路由
    $route = [$controller$action];
    //判断路由,是否存在 不存在则弹出未找到路由
    if ($this->hasDefinedRoute($route)) {
        throw new HttpException(404, 'invalid request:' str_replace('|'$depr$url));
    }
    //返回路由
    return $route;
posted @ 2021-10-29 09:00  青竹之下  阅读(81)  评论(0编辑  收藏  举报