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)) {
109 $this->basePath = $basePath;
123 if (!is_string($cacheFile) && $cacheFile !==
false) {
127 $this->cacheFile = $cacheFile;
129 if ($cacheFile !==
false && !is_writable(dirname($cacheFile))) {
158 if (!is_string($pattern)) {
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()) {
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)) {
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);
getMethod()
Retrieves the HTTP method of the request.
dispatch(ServerRequestInterface $request)
Dispatch router for HTTP request.
Representation of an incoming, server-side HTTP request.
foreach($paths as $path) $request
getNamedRoute($name)
Get named route object.
pushGroup($pattern, $callable)
Add a route group to the array.
getRoutes()
Get route objects.
__construct(RouteParser $parser=null)
Create new router.
relativePathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route excluding the base path.
setCacheFile($cacheFile)
Set path to fast route cache file.
Parses route strings of the following form:
setDispatcher(Dispatcher $dispatcher)
Describes the interface of a container that exposes methods to read its entries.
getUri()
Retrieves the URI instance.
urlFor($name, array $data=[], array $queryParams=[])
Build the path for a named route.
removeNamedRoute($name)
Remove named route.
popGroup()
Removes the last route group from the array.
map($methods, $pattern, $handler)
Add route.
createRoute($methods, $pattern, $callable)
Create a new Route object.
processGroups()
Process route groups.
pathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route including the base path.
Slim Framework (https://slimframework.com)
setBasePath($basePath)
Set the base path used in pathFor()
setContainer(ContainerInterface $container)