频道栏目
首页 > 资讯 > 其他 > 正文

Slim研读笔记:路由

17-12-26        来源:[db:作者]  
收藏   我要投稿

这节,我们承接上节继续研读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);

相关TAG标签
上一篇:golang安装步骤详情
下一篇:一个多态经典实例
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站