ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] # 说明 上一篇, 我们分析了中间件的执行过程,但对于中间件最里面一层还未展开分析,接下来三节将对其展开分析。`runWithRequest`方法: ``` protected function runWithRequest(Request $request) { . . . 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`类的实例。该实例大概长这样: ![](https://img.kancloud.cn/4d/18/4d18892490a648eae75f4a2c5be38849_336x237.PNG)