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





阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 奇瑞艾瑞泽7自动挡 奇瑞瑞虎汽车报价 新qq 艾瑞泽 艾瑞泽m7 电动轿车 瑞虎汽车 qirui qq车报价 奇端汽车报价 瑞虎5 qq汽车报价 奇端汽车报价及图片集 瑞虎1 qq汽车 瑞虎3怎么样 qq3 奇端 qq3汽车 艾瑞泽7 奇锐 qq车多少钱 汽车可以0首付吗 省油 瑞虎3如何 400 瑞虎的价格 瑞虎汽车怎么样 北汽威旺m30多少钱 4s店全款买车流程 深度 新能源汽车报价 试驾 奇田热水器 奇田 热水器 奇田燃气灶 奇田 奇田灶具 奇田热水器sms-40 奇田热水器使用说明书 奇田燃气热水器