先标记觉得以后会用到的内容:

// add route to the request's attributes in case a middleware or handler needs access to the route
$request = $request->withAttribute('route', $route);

或许以后可以在Middleware中拿到route做些其他的事情。

  上篇已经分析到route是在APP的__invoke()中被调用的,这里来看看怎么匹配route的。大概的调用过程如下:

APP->__invoke()
     \
$request = $this->dispatchRouterAndPrepareRoute($request, $router);
     \
$routeInfo = $router->dispatch($request);
     \
$this->createDispatcher()->dispatch($request->getMethod(),$uri); (告知是否找到,并且给出route的id)
     \
\FastRoute\simpleDispatcher(function (RouteCollector $r){}) (将所有的route存放在RouteCollector中,并且返回GroupCountBased)
     \
foreach ($this->getRoutes() as $route) {
    $r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier());
}
 
  在APP __invoke里会去寻找匹配的route。route在router中,自然要去routers中去寻找,调用了 $router->dispatch($request)。在router的dispatch中会创建一个Dispatcher,$this->createDispatcher()。
  在createDispatcher()会使用一个命名空间公共函数\FastRoute\simpleDispatcher()。它将每个route的方法(get/post)、pattern、identify组成一个数组放入 RouteCollector 中,再通过GroupCountBased的构造方法将RouteCollector的数据都传入GroupCountBased。$this->createDispatcher()->dispatch($request->getMethod(),$uri)的dispatch其实是GroupCountBased的dispatch()方法。
  GroupCountBased->dispatch($request->getMethod(),$uri)会匹配相应的route。这匹配的办法其实很简单,就是判断所传入的method与uri是否在数组中能取到($this->staticRouteMap[$httpMethod][$uri])。返回匹配到与否的标志、route的identify。
  App->dispatchRouterAndPrepareRoute()会将返回的route匹配结果保留并存入到requst 中,这样以后再request中就直接可以拿到对应的route了,想起来棒棒的,但是肿么拿呢。
  APP->__invoke()执行进行route的执行:
if ($routeInfo[0] === Dispatcher::FOUND) {
$route = $router->lookupRoute($routeInfo[1]);
return $route->run($request, $response);
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
if (!$this->container->has('notAllowedHandler')) {
throw new MethodNotAllowedException($request, $response, $routeInfo[1]);
}
/** @var callable $notAllowedHandler */
$notAllowedHandler = $this->container->get('notAllowedHandler');
return $notAllowedHandler($request, $response, $routeInfo[1]);
}

  $route = $router->lookupRoute($routeInfo[1]); 通过route的identify找到route。

  route->run执行route。

  run的结果最后就是执行自己的__invoke,从所有的middleware开始执行,栈的最后一个元素是自己。

    /**
* Dispatch route callable against current Request and Response objects
*
* This method invokes the route object's callable. If middleware is
* registered for the route, each callable middleware is invoked in
* the order specified.
*
* @param ServerRequestInterface $request The current Request object
* @param ResponseInterface $response The current Response object
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception if the route callable throws an exception
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
{
//debug_print_backtrace();
$this->callable = $this->resolveCallable($this->callable); /** @var InvocationStrategyInterface $handler */
$handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse(); // invoke route callable
if ($this->outputBuffering === false) {
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
} else {
try {
ob_start();
$newResponse = $handler($this->callable, $request, $response, $this->arguments);
$output = ob_get_clean();
} catch (Exception $e) {
ob_end_clean();
throw $e;
}
} if ($newResponse instanceof ResponseInterface) {
// if route callback returns a ResponseInterface, then use it
$response = $newResponse;
} elseif (is_string($newResponse)) {
// if route callback returns a string, then append it to the response
if ($response->getBody()->isWritable()) {
$response->getBody()->write($newResponse);
}
} if (!empty($output) && $response->getBody()->isWritable()) {
if ($this->outputBuffering === 'prepend') {
// prepend output buffer content
$body = new Http\Body(fopen('php://temp', 'r+'));
$body->write($output . $response->getBody());
$response = $response->withBody($body);
} elseif ($this->outputBuffering === 'append') {
// append output buffer content
$response->getBody()->write($output);
}
} return $response;
}
  $this->callable = $this->resolveCallable($this->callable);用来将class::method的callback组成正常的callback。
  $handler 是找到的策略,在handler会执行 call_user_func(),也就是route的闭包函数被执行了,也就是route此时被执行。 这样将得到reponse返回或者将得到的字符串结果写入网页中。
 

学习Slim Framework for PHP v3 (六)--route怎么被匹配的?的更多相关文章

  1. 学习Slim Framework for PHP v3 (五)--route怎么被调用的?

    上一篇中分析了get()如何加入新的route的,这篇来分析route是如何被调用的. 首先,route是在routers里保存,router有在container中存放.container提供了ge ...

  2. 学习Slim Framework for PHP v3 (七)--route middleware怎么被add进来的?

    上两篇中分析了route是怎么被加进来的,以及如何被匹配的.这篇说一下route middleware是如何被加进来的,即add进来的.index.php的代码如下: $app->get('/f ...

  3. 学习Slim Framework for PHP v3 (四)--get()是怎么加进去的?

    看看官网加粗的一句话: At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate ...

  4. 学习Slim Framework for PHP v3 (三)

    继续上一篇的问题,如何动态的添加不同的Module.添加Module是给Middleware用的,用于调用Module的写日志方法.上篇中的写法是在app->add(mv),这时的middlew ...

  5. 学习Slim Framework for PHP v3 ( 二)

    昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...

  6. Migration from Zend Framework v2 to v3

    Migration from Zend Framework v2 to v3 Zend Framework v2 to v3 has been intended as an incremental u ...

  7. 快乐学习 Ionic Framework+PhoneGap 手册1-1{创建APP项目}

    快乐学习 Ionic Framework+PhoneGap 手册1-1 * 前提必须安装 Node.js,安装PhoneGap,搭建Android开发环境,建议使用真机调试 {1.1}= 创建APP项 ...

  8. 学习Entity Framework 中的Code First

    这是上周就写好的文章,是在公司浩哥的建议下写的,本来是部门里面分享求创新用的,这里贴出来分享给大家. 最近在对MVC的学习过程中,接触到了Code First这种新的设计模式,感觉很新颖,并且也体验到 ...

  9. 转载:学习Entity Framework 中的Code First

    看完觉得不错,适合作为学习资料,就转载过来了 原文链接:http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html 这是上周就写 ...

随机推荐

  1. [转帖]ExtJs与服务器的交互(一)

    Ext是一个非常好的Ajax框架,其华丽的外观表现确实令我们折服,然而一个应用始终离开不服务器端,因此在实现开发中我们需要对Ext与服务器端的交互技术有较为详细的了解,在开发中才能游刃有余地应用.本文 ...

  2. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  3. axTE3DWindowEx双屏对比控件白屏解决方法以及网上方法的校正(CreateControlOveride)

    环境:vs2012,TE 6.5.1,winfrom C# 要做skyline的双屏显示功能,网上找到方法是用axTE3DWindowEx控件实现,把控件拖进去,运行,发现axTE3DWindow是正 ...

  4. C++ 初始化与赋值

    1.初始化与赋值的区别: 二者的区别不是看,是否有=这个赋值操作符,而是看操作的时候,对象是否已经有值. 初始化:创建对象,并给它设置初始值. 赋值:对象已经有值,擦除对象的当前值,并使用新值代替. ...

  5. Codeforces Gym 100803F There is No Alternative 暴力Kruskal

    There is No Alternative 题目连接: http://codeforces.com/gym/100803/attachments Description ICPC (Isles o ...

  6. Delphi开发OCX详细步骤总结

     首先要弄明白你要写的OCX是用在客户端还是用在服务器端    假如用在客户端: 1.创建  打开delphi 7,选择菜单"new"->"other"- ...

  7. BBOSS框架使用jquery方式传參到后台的时候,要注意的事项

    BBOSS框架.从前台传到后台的时候,參数要以这样的方式: public String initAddOrModExtendUser(HttpServletRequest request,       ...

  8. java追加文件的几种方式

    import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...

  9. ztree使用系列四(ztree实现同级拖拽排序并将排序结果保存数据库)

    ztree这个系列的最后一篇,也是ztree功能强大的体现之中的一个--排序功能. ztree能够实现全部节点之间任意的拖拽排序功能.我这里依据须要实现了仅仅同意同级之间任意拖拽排序,事实上原理都一样 ...

  10. Linux下MySQL使用

    Linux刚安装完并不是安装了全部的MySQL,比如Centos就没有安装mysql-server. 故使用rpm -q mysql会看到明明安装了mysql却用不了. 所以先安装mysql-serv ...