ThinkPHP6 核心分析(六):Url 解析

说明

更新日志:2019-10-28 更新到6.0正式版

接第四篇,继续分析Http类的run方法里面调用的runWithRequest方法:

protected function runWithRequest(Request $request)
{
    .
    .
    .
    // 设置开启事件机制
    $this->app->event->withEvent($this->app->config->get('app.with_event', true));

    // 监听HttpRun
    $this->app->event->trigger(HttpRun::class);

    return $this->app->middleware->pipeline()
            ->send($request)
            ->then(function ($request) {
                return $this->dispatchToRoute($request);
            });
}

这里从$this->dispatchToRoute($request)展开分析,在配置文件中关闭路由解析,即在config/app.php中将 with_route设置为false。以下分析假设访问的URL为www.tp6.com/demo/hello,并创建好了Demo控制器和hello操作——其代码如下:

<?php
namespace app\controller;

use app\BaseController;

class Demo extends BaseController
{
    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

}

Url解析

dispatchToRoute方法:

protected function dispatchToRoute($request)
{
    $withRoute = $this->app->config->get('app.with_route', true) ? function () {
        $this->loadRoutes();
    } : null;

    return $this->app->route->dispatch($request, $withRoute);
}

主要逻辑都在dispatch方法:

public function dispatch(Request $request, $withRoute = null)
{
    $this->request = $request;
    $this->host    = $this->request->host(true);
    //加载路由配置、缓存等
    $this->init();

    if ($withRoute) {
        //加载路由
        $withRoute();
        $dispatch = $this->check();
    } else {
        //如果没有开启路由,将执行这里的语句
        //$this->path()得到PATHINFO,比如/demo/hello
        $dispatch = $this->url($this->path());
    }
    // $dispatch是think\route\dispatch\Url的实例,该类继承了Controller类
    // 且该类中没有init方法,所以这里执行的是其父类的init方法
    $dispatch->init($this->app);

    return $this->app->middleware->pipeline('route')
            ->send($request)
            ->then(function () use ($dispatch) {
                return $dispatch->run();
            });
}

因为前面设置关闭了路由解析,所以这里执行到else的语句:$dispatch = $this->url($this->path());,解析Url。Url方法如下:

public function url(string $url): UrlDispatch
{
    // 第三个参数为PATHINFO
    return new UrlDispatch($this->request, $this->group, $url);
}

该方法新建并返回一个think\route\dispatch\Url类的实例。我们来看看这个实例化都做了些什么。think\route\dispatch\Url类的构造函数:

public function __construct(Request $request, Rule $rule, $dispatch, array $param = [], int $code = null)
{
    $this->request = $request;
    $this->rule    = $rule;
    // 解析默认的URL规则
    $dispatch = $this->parseUrl($dispatch);

    parent::__construct($request, $rule, $dispatch, $this->param, $code);
}

最主要的逻辑是在parseUrl方法:

protected function parseUrl(string $url): array
{
    // 获取URL分隔符
    $depr = $this->rule->config('pathinfo_depr');
    // $this->rule->getRouter() 获取路由对象
    // 获取路由绑定
    $bind = $this->rule->getRouter()->getDomainBind();

    if ($bind && preg_match('/^[a-z]/is', $bind)) {
        $bind = str_replace('/', $depr, $bind);
        // 如果有模块/控制器绑定
        $url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr);
    }
    // $path为[控制器,操作]这样组成的数组,如["demo", "hello"]
    $path = $this->rule->parseUrlPath($url);
    if (empty($path)) {
        return [null, null];
    }

    // 解析控制器
    // 获取控制器名
    $controller = !empty($path) ? array_shift($path) : null;
    // 检查控制器是否合法
    // 正则匹配:开头是字母,后面是0个到多个字母、下划线或者点
    if ($controller && !preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) {
        throw new HttpException(404, 'controller not exists:' . $controller);
    }

    // 解析操作
    $action = !empty($path) ? array_shift($path) : null;
    $var    = [];

    // 解析额外参数
    if ($path) {
        // 正则匹配:一个至多个字母,下划线,中间是‘|’,结尾是非‘|’的任意字符,比如 hello|123
        preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$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;
}

整个过程有点繁琐,分析见注释,该方法最后返回一个控制器和操作组成的数组,大概类似这样:[$controller, $action]

最终,dispatch方法的$dispatch = $this->url($this->path());语句得到一个think\route\dispatch\Url类的实例。该实例大概长这样:

ThinkPHP6 源码阅读(六):Url解析

本作品采用《CC 协议》,转载必须注明作者和本文链接
Was mich nicht umbringt, macht mich stärker
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!