学习Slim Framework for PHP v3 (七)--route middleware怎么被add进来的?
上两篇中分析了route是怎么被加进来的,以及如何被匹配的。这篇说一下route middleware是如何被加进来的,即add进来的。index.php的代码如下:
$app->get('/forbase', function ($request, $response, $args){
Example\Module\Base::instance()->init($request,$response);
return $response;
})->add(Example\MiddleWare\MyMiddleware::instance(Example\Module\Base::instance()));
首先,确定的是$app->get()返回的是一个route类型变量,即接下来的add方法是属于route 的。
Route类的定义:
/**
* Route
*/
class Route extends Routable implements RouteInterface
{
use MiddlewareAwareTrait;
继承了abstract Routable类,并实现了RouteInterface 接口。这个add方法是Routable抽象类的方法。
/**
* Prepend middleware to the middleware collection
*
* @param mixed $callable The callback routine
*
* @return static
*/
public function add($callable)
{
$callable = $this->resolveCallable($callable);
if ($callable instanceof Closure) {
$callable = $callable->bindTo($this->container);
} $this->middleware[] = $callable;
return $this;
}
将middleware加入了一个collection中了。在route被调用run的时候,就会将collection里的middleware都加入到middleware stack中。最后条用callMiddlewarestack()从栈顶执行到栈底。
/**
* Run route
*
* This method traverses the middleware stack, including the route's callable
* and captures the resultant HTTP response object. It then sends the response
* back to the Application.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function run(ServerRequestInterface $request, ResponseInterface $response)
{
// Finalise route now that we are about to run it
$this->finalize(); // Traverse middleware stack and fetch updated response
return $this->callMiddlewareStack($request, $response);
}
$this->finalize(); 实现了将collection 中的middleware都加入到 middlewarestack中。
/**
* Finalize the route in preparation for dispatching
*/
public function finalize()
{
if ($this->finalized) {
return;
} $groupMiddleware = [];
foreach ($this->getGroups() as $group) {
$groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
} $this->middleware = array_merge($this->middleware, $groupMiddleware); foreach ($this->getMiddleware() as $middleware) {
$this->addMiddleware($middleware);
} $this->finalized = true;
}
最后callMiddlewareStack的时候会被执行。
从下一篇开始读读每一行代码,比这样走流程更细一些。只是,不知道我这样的方法是不是对的,和最效率的。求高人传授更好方法,先谢过。
学习Slim Framework for PHP v3 (七)--route middleware怎么被add进来的?的更多相关文章
- 学习Slim Framework for PHP v3 (五)--route怎么被调用的?
上一篇中分析了get()如何加入新的route的,这篇来分析route是如何被调用的. 首先,route是在routers里保存,router有在container中存放.container提供了ge ...
- 学习Slim Framework for PHP v3 (六)--route怎么被匹配的?
先标记觉得以后会用到的内容: // add route to the request's attributes in case a middleware or handler needs access ...
- 学习Slim Framework for PHP v3 (四)--get()是怎么加进去的?
看看官网加粗的一句话: At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate ...
- 学习Slim Framework for PHP v3 (三)
继续上一篇的问题,如何动态的添加不同的Module.添加Module是给Middleware用的,用于调用Module的写日志方法.上篇中的写法是在app->add(mv),这时的middlew ...
- 学习Slim Framework for PHP v3 ( 二)
昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...
- 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 ...
- 跟着刚哥学习Spring框架--事务配置(七)
事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...
- 快乐学习 Ionic Framework+PhoneGap 手册1-1{创建APP项目}
快乐学习 Ionic Framework+PhoneGap 手册1-1 * 前提必须安装 Node.js,安装PhoneGap,搭建Android开发环境,建议使用真机调试 {1.1}= 创建APP项 ...
- 学习Entity Framework 中的Code First
这是上周就写好的文章,是在公司浩哥的建议下写的,本来是部门里面分享求创新用的,这里贴出来分享给大家. 最近在对MVC的学习过程中,接触到了Code First这种新的设计模式,感觉很新颖,并且也体验到 ...
随机推荐
- linux集群管理<转>
云在根本上是由硬件和软件组成的,这些组件需要经常细心地维护.出现故障的硬件需要修理或更换:软件需要应用补丁.更新和升级:必须根据需求和潜在的安全威胁提前配置系统.应用程序开发人员可能觉得计算云很方便. ...
- Slony-I中对storelisten出错的处理
客户质询的现象是: Slony-I运行中,log中发现FATAL信息: FATAL storeListen: unknown node ID 出现了上述错误后,再看后继的log,又恢复正常运行了. 客 ...
- jQuery Mobile和PhoneGap混合开发
其实二者并不影响,PhoneGap负责调用系统的接口,jQuery Mobile实现一些网页效果.PhoneGap开发请看上一篇文章,jQuery Mobile开发环境搭建如下:[请先阅读上一篇文章, ...
- hdu 5276 YJC tricks time 数学
YJC tricks time Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- TXT EXPLORER
EXE程序:http://files.cnblogs.com/xe2011/RELEASE_TXTExplorer2014-08-25-165323.rar c# 源码:http://files.cn ...
- android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索
最近跟着百度地图API学地图开发,先是学了路径搜索,对于已知坐标的两点进行驾车.公交.步行三种路径的搜索(公交路径运行没效果,待学习中),后来又 学了定位功能,能够获取到自己所在位置的经纬度,但当将两 ...
- [MEAN+ Webstrom] First API -- 2.Debug Node.js RESTful application
Using WebStrom can easily debug the Node applcation. For example, we have an Node+Express applicatio ...
- iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController)
iOS开发——实战OC篇&环境搭建之Xib(玩转UINavigationController与UITabBarController) 前面我们介绍了StoryBoard这个新技术,和纯技术 ...
- DOS命令如何进入指定的下一级目录?
如cd E:\java workspace\test\src 如何进入E盘 ,直接 E:
- MR-为什么MR类的变量喜欢定义在方法外面?
写了一些MR后发现不管是别人写的还是官方给的,比如map方法,变量都是定义在方法外面. 因为之前都是照猫画虎的写的,所以自己也一直这么写,可是为什么呢? 我试了试定义在方法里面,程序照样跑,试了几个程 ...