13use InvalidArgumentException;
 
   52    protected $basePath = 
'';
 
   59    protected $cacheFile = 
false;
 
   66    protected $routes = [];
 
   72    protected $routeCounter = 0;
 
   79    protected $routeGroups = [];
 
   93        $this->routeParser = 
$parser ?: 
new StdParser;
 
  105        if (!is_string($basePath)) {
 
  106            throw new InvalidArgumentException(
'Router basePath must be a string');
 
  109        $this->basePath = $basePath;
 
  123        if (!is_string($cacheFile) && $cacheFile !== 
false) {
 
  124            throw new InvalidArgumentException(
'Router cacheFile must be a string or false');
 
  127        $this->cacheFile = $cacheFile;
 
  129        if ($cacheFile !== 
false && !is_writable(dirname($cacheFile))) {
 
  130            throw new RuntimeException(
'Router cacheFile directory must be writable');
 
  158        if (!is_string($pattern)) {
 
  159            throw new InvalidArgumentException(
'Route pattern must be a string');
 
  163        if ($this->routeGroups) {
 
  164            $pattern = $this->processGroups() . $pattern;
 
  168        $methods = array_map(
"strtoupper", $methods);
 
  171        $route = $this->createRoute($methods, $pattern, 
$handler);
 
  172        $this->routes[$route->getIdentifier()] = $route;
 
  173        $this->routeCounter++;
 
  189        $uri = 
'/' . ltrim(
$request->getUri()->getPath(), 
'/');
 
  191        return $this->createDispatcher()->dispatch(
 
  208        $route = 
new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter);
 
  209        if (!empty($this->container)) {
 
  210            $route->setContainer($this->container);
 
  221        if ($this->dispatcher) {
 
  222            return $this->dispatcher;
 
  226            foreach ($this->getRoutes() as $route) {
 
  227                $r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier());
 
  231        if ($this->cacheFile) {
 
  232            $this->dispatcher = \FastRoute\cachedDispatcher($routeDefinitionCallback, [
 
  233                'routeParser' => $this->routeParser,
 
  234                'cacheFile' => $this->cacheFile,
 
  237            $this->dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback, [
 
  238                'routeParser' => $this->routeParser,
 
  242        return $this->dispatcher;
 
  250        $this->dispatcher = $dispatcher;
 
  260        return $this->routes;
 
  274        foreach ($this->routes as $route) {
 
  275            if (
$name == $route->getName()) {
 
  279        throw new RuntimeException(
'Named route does not exist for name: ' . 
$name);
 
  291        $route = $this->getNamedRoute(
$name);
 
  294        unset($this->routes[$route->getIdentifier()]);
 
  305        foreach ($this->routeGroups as $group) {
 
  306            $pattern .= $group->getPattern();
 
  322        array_push($this->routeGroups, $group);
 
  333        $group = array_pop($this->routeGroups);
 
  334        return $group instanceof 
RouteGroup ? $group : 
false;
 
  343        if (!isset($this->routes[$identifier])) {
 
  344            throw new RuntimeException(
'Route not found, looks like your route cache is stale.');
 
  346        return $this->routes[$identifier];
 
  363        $route = $this->getNamedRoute(
$name);
 
  364        $pattern = $route->getPattern();
 
  366        $routeDatas = $this->routeParser->parse($pattern);
 
  371        $routeDatas = array_reverse($routeDatas);
 
  374        foreach ($routeDatas as $routeData) {
 
  375            foreach ($routeData as $item) {
 
  376                if (is_string($item)) {
 
  383                if (!array_key_exists($item[0], 
$data)) {
 
  388                    $segmentName = $item[0];
 
  391                $segments[] = 
$data[$item[0]];
 
  393            if (!empty($segments)) {
 
  400        if (empty($segments)) {
 
  401            throw new InvalidArgumentException(
'Missing data for URL segment: ' . $segmentName);
 
  403        $url = implode(
'', $segments);
 
  406            $url .= 
'?' . http_build_query($queryParams);
 
  429        if ($this->basePath) {
 
  452        trigger_error(
'urlFor() is deprecated. Use pathFor() instead.', E_USER_DEPRECATED);
 
  453        return $this->pathFor(
$name, 
$data, $queryParams);
 
foreach($paths as $path) $request
An exception for terminatinating execution or to throw for unit testing.
Parses route strings of the following form:
setContainer(ContainerInterface $container)
getRoutes()
Get route objects.
getNamedRoute($name)
Get named route object.
processGroups()
Process route groups.
map($methods, $pattern, $handler)
Add route.
pushGroup($pattern, $callable)
Add a route group to the array.
pathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route including the base path.
createRoute($methods, $pattern, $callable)
Create a new Route object.
setBasePath($basePath)
Set the base path used in pathFor()
setDispatcher(Dispatcher $dispatcher)
__construct(RouteParser $parser=null)
Create new router.
popGroup()
Removes the last route group from the array.
dispatch(ServerRequestInterface $request)
Dispatch router for HTTP request.
setCacheFile($cacheFile)
Set path to fast route cache file.
urlFor($name, array $data=[], array $queryParams=[])
Build the path for a named route.
relativePathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route excluding the base path.
removeNamedRoute($name)
Remove named route.
Describes the interface of a container that exposes methods to read its entries.
Describes the interface of a container that exposes methods to read its entries.
Representation of an incoming, server-side HTTP request.
Slim Framework (https://slimframework.com)