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

来源:互联网 发布:软件交付需求变更 编辑:程序博客网 时间:2024/06/06 16:49

导读:今天我们继续学习Router类

先看下setBasePath方法

public function setBasePath($basePath){        if (!is_string($basePath)) {            throw new InvalidArgumentException('Router basePath must be a string');        }        $this->basePath = $basePath;        return $this;    }

设置了一个basePath,只看到在测试时使用。并未在其他地方看到使用

setContainer 方法

public function setContainer(ContainerInterface $container){   $this->container = $container;}

看看调用的位置

Slim/DefaultServicesProvider.php就在容器的注册里面$container['router'] = function ($container) {                $routerCacheFile = false;                if (isset($container->get('settings')['routerCacheFile'])) {                    $routerCacheFile = $container->get('settings')['routerCacheFile'];                }                $router = (new Router)->setCacheFile($routerCacheFile);                if (method_exists($router, 'setContainer')) {                    $router->setContainer($container);                }                return $router;            };

map 重点

 /**     * Add route     *     //http 方法     * @param  string[] $methods Array of HTTP methods     // url地址     * @param  string   $pattern The route pattern     * 回调函数     * @param  callable $handler The route callable     *     * @return RouteInterface     *     * @throws InvalidArgumentException if the route pattern isn't a string     */    public function map($methods, $pattern, $handler)    {        if (!is_string($pattern)) {            throw new InvalidArgumentException('Route pattern must be a string');        }        // Prepend parent group pattern(s)        if ($this->routeGroups) {            $pattern = $this->processGroups() . $pattern;        }        // According to RFC methods are defined in uppercase (See RFC 7231)        $methods = array_map("strtoupper", $methods);        // Add route        $route = $this->createRoute($methods, $pattern, $handler);        $this->routes[$route->getIdentifier()] = $route;        $this->routeCounter++;        return $route;    }

map牵扯的点比较多,明天继续分析