导读:今天我们继续学习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牵扯的点比较多,明天继续分析