Slim 框架学习,第十九天 _Router(十)

今天我们继续学习 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
  }
}

最终返回了,我们的路由响应的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值