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

导读:今天继续学习Slim/Route.php 类剩余的功能。前面的章节,已经对每个方法都说过了。今天只剩一个__invoke 方法了

__invoke

/**
     * 根据当前请求和响应对象 调用 能调用的路由
     * 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)
    {
        //这里得到的是容器的闭包对象

        /**
         * object(Closure)#19 (2) {
            ["this"]=>
            object(Slim\Container)#3 (7) {
            ["defaultSettings":"Slim\Container":private]=>
            array(7) {
            ["httpVersion"]=>
            string(3) "1.1"
            ["responseChunkSize"]=>
            int(4096)
            ["outputBuffering"]=>
            string(6) "append"
            ["determineRouteBeforeAppMiddleware"]=>
            bool(false)
            ["displayErrorDetails"]=>
            bool(false)
            ["addContentLengthHeader"]=>
            bool(true)
            ["routerCacheFile"]=>
            bool(false)
            }
         **/
        $this->callable = $this->resolveCallable($this->callable);

        /** @var InvocationStrategyInterface $handler */
        /**
         *  ["foundHandler"]=>
                object(Closure)#12 (1) {
                    ["this"]=>
                        object(Slim\DefaultServicesProvider)#7 (0) {
                    }
                }
         */
        $handler = isset($this->container) ? $this->container->get('foundHandler') : new RequestResponse();

        $newResponse = $handler($this->callable, $request, $response, $this->arguments);

        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);
            }
        }
        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
  }
}

最终返回的是 Slim\Http\Response 对象

至此,该类就学完了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值