Slim研读笔记八之路由(中)
来源:互联网 发布:剑桥大学生活费 知乎 编辑:程序博客网 时间:2024/05/22 00:18
这节,我们承接上节继续研读Slim路由部分。我们首先由map函数进行延伸,因为get()、post()、any()等函数均是封装的此函数。
/** * 使用多种请求方法增加路由 * Add route with multiple methods * * @param string[] $methods Numeric array of HTTP method names * @param string $pattern The route URI pattern * @param callable|string $callable The route callback routine * * @return RouteInterface */ public function map(array $methods, $pattern, $callable) { // 回调函数实现自闭包类 if ($callable instanceof Closure) { $callable = $callable->bindTo($this->container); } // Slim默认服务曾注册了Router服务。这里我们返回了router对象的map()方法。 $route = $this->container->get('router')->map($methods, $pattern, $callable); // 在注册router时,曾使用过$router->setContainer($container)方法,这里是不是有重复? // 还是有其他寓意呢? if (is_callable([$route, 'setContainer'])) { $route->setContainer($this->container); } // 设置输出缓冲模式 if (is_callable([$route, 'setOutputBuffering'])) { $route->setOutputBuffering($this->container->get('settings')['outputBuffering']); } // 返回处理后的路由实例 return $route; }
首先看第一段,该段意思是复制当前$callable闭包对象,绑定指定的$this对象到$this->container对象中。也就是说$callable闭包函数中的$this等价于$this->container中的$this。
// 回调函数实现自闭包类 if ($callable instanceof Closure) { $callable = $callable->bindTo($this->container); }
查看下一段,前面我们曾在app构造函数中注册了路由,然我们继续进入路由查看
// Slim默认服务曾注册了Router服务。这里我们返回了router对象的map()方法。 $route = $this->container->get('router')->map($methods, $pattern, $callable);
阅读全文