由于明天要早起滑雪,所以要早睡。后天补上,抱歉
现在开始补周五的博客
今天我们继续学习 Slim/Route.php 类的内容
先看一下 finalize 方法
/**
* 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;
}
这个方法做了两件事情
- 准备好了我们用到了的中间件
- 记录一下是否完成了调度前的路由准备,如果准备过了。就不用在准备了。
我们顺便看一下finalize方法是在哪里被调用的
Slim/Route.php
/**
* 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);
}
在路由run的时候调用的该方法
继续看 getCallable 方法
//得到我们的回调函数
public function getCallable()
{
return $this->callable;
}
setCallable 设置回调函数
/**
* This method enables you to override the Route's callable
*
* @param string|\Closure $callable
*/
public function setCallable($callable)
{
$this->callable = $callable;
}
今天先到这里,明天继续