今天我们继续学习 Slim/Route.php 类剩余的内容. 一些get ,set 的方法就不说了。挑重点说下
先看下 run 方法
/**
* Run route
*
* 该方法遍历中间件堆栈,包括路由的回调并捕获生成的http响应对 象。然后将他们返回给应用程序
* 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);
}
finalize 方法前面已经讲述过了,今天主要学习下callMiddlewareStack,看对中间件堆栈做了什么
看一下 callMiddlewareStack 方法
Slim/MiddlewareAwareTrait.php
public function callMiddlewareStack(ServerRequestInterface $request, ResponseInterface $response)
{
if (is_null($this->tip)) {
$this->seedMiddlewareStack();
}
/** @var callable $start */
$start = $this->tip; //其实这里就是我们的容器对象
$this->middlewareLock = true;
$response = $start($request, $response);
$this->middlewareLock = false;
return $response;
}
我们看下最后的返回值
object(Slim\Http\Response)#26 (5) {
["status":protected]=>
int(200)
["reasonPhrase":protected]=>
string(0) ""
["protocolVersion":protected]=>
string(3) "1.1"
["headers":protected]=>
object(Slim\Http\Headers)#27 (1) {
["data":protected]=>
array(1) {
["content-type"]=>
array(2) {
["value"]=>
array(1) {
[0]=>
string(24) "text/html; charset=UTF-8"
}
["originalKey"]=>
string(12) "Content-Type"
}
}
}
["body":protected]=>
object(Slim\Http\Body)#25 (7) {
["stream":protected]=>
resource(37) of type (stream)
["meta":protected]=>
array(6) {
["wrapper_type"]=>
string(3) "PHP"
["stream_type"]=>
string(4) "TEMP"
["mode"]=>
string(3) "w+b"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(true)
["uri"]=>
string(10) "php://temp"
}
["readable":protected]=>
NULL
["writable":protected]=>
bool(true)
["seekable":protected]=>
NULL
["size":protected]=>
NULL
["isPipe":protected]=>
NULL
}
}
最终返回了,我们的路由响应的数据