ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Slim\Router Class Reference

Router. More...

+ Inheritance diagram for Slim\Router:
+ Collaboration diagram for Slim\Router:

Public Member Functions

 __construct (RouteParser $parser=null)
 Create new router. More...
 
 setBasePath ($basePath)
 Set the base path used in pathFor() More...
 
 setCacheFile ($cacheFile)
 Set path to fast route cache file. More...
 
 setContainer (ContainerInterface $container)
 
 map ($methods, $pattern, $handler)
 Add route. More...
 
 dispatch (ServerRequestInterface $request)
 Dispatch router for HTTP request. More...
 
 setDispatcher (Dispatcher $dispatcher)
 
 getRoutes ()
 Get route objects. More...
 
 getNamedRoute ($name)
 Get named route object. More...
 
 removeNamedRoute ($name)
 Remove named route. More...
 
 pushGroup ($pattern, $callable)
 Add a route group to the array. More...
 
 popGroup ()
 Removes the last route group from the array. More...
 
 lookupRoute ($identifier)
 
 relativePathFor ($name, array $data=[], array $queryParams=[])
 Build the path for a named route excluding the base path. More...
 
 pathFor ($name, array $data=[], array $queryParams=[])
 Build the path for a named route including the base path. More...
 
 urlFor ($name, array $data=[], array $queryParams=[])
 Build the path for a named route. More...
 
 map ($methods, $pattern, $handler)
 Add route. More...
 
 dispatch (ServerRequestInterface $request)
 Dispatch router for HTTP request. More...
 
 pushGroup ($pattern, $callable)
 Add a route group to the array. More...
 
 popGroup ()
 Removes the last route group from the array. More...
 
 getNamedRoute ($name)
 Get named route object. More...
 
 lookupRoute ($identifier)
 
 relativePathFor ($name, array $data=[], array $queryParams=[])
 Build the path for a named route excluding the base path. More...
 
 pathFor ($name, array $data=[], array $queryParams=[])
 Build the path for a named route including the base path. More...
 

Protected Member Functions

 createRoute ($methods, $pattern, $callable)
 Create a new Route object. More...
 
 createDispatcher ()
 
 processGroups ()
 Process route groups. More...
 

Protected Attributes

 $container
 
 $routeParser
 
 $basePath = ''
 
 $cacheFile = false
 
 $routes = []
 
 $routeCounter = 0
 
 $routeGroups = []
 
 $dispatcher
 

Additional Inherited Members

- Data Fields inherited from Slim\Interfaces\RouterInterface
const DISPATCH_STATUS = 0
 
const ALLOWED_METHODS = 1
 

Detailed Description

Router.

This class organizes Slim application route objects. It is responsible for registering route objects, assigning names to route objects, finding routes that match the current HTTP request, and creating URLs for a named route.

Definition at line 31 of file Router.php.

Constructor & Destructor Documentation

◆ __construct()

Slim\Router::__construct ( RouteParser  $parser = null)

Create new router.

Parameters
RouteParser$parser

Definition at line 91 of file Router.php.

92 {
93 $this->routeParser = $parser ?: new StdParser;
94 }
$parser
Definition: BPMN2Parser.php:23

References $parser.

Member Function Documentation

◆ createDispatcher()

Slim\Router::createDispatcher ( )
protected
Returns
\FastRoute\Dispatcher

Definition at line 219 of file Router.php.

220 {
221 if ($this->dispatcher) {
222 return $this->dispatcher;
223 }
224
225 $routeDefinitionCallback = function (RouteCollector $r) {
226 foreach ($this->getRoutes() as $route) {
227 $r->addRoute($route->getMethods(), $route->getPattern(), $route->getIdentifier());
228 }
229 };
230
231 if ($this->cacheFile) {
232 $this->dispatcher = \FastRoute\cachedDispatcher($routeDefinitionCallback, [
233 'routeParser' => $this->routeParser,
234 'cacheFile' => $this->cacheFile,
235 ]);
236 } else {
237 $this->dispatcher = \FastRoute\simpleDispatcher($routeDefinitionCallback, [
238 'routeParser' => $this->routeParser,
239 ]);
240 }
241
242 return $this->dispatcher;
243 }
getRoutes()
Get route objects.
Definition: Router.php:258
$r
Definition: example_031.php:79

References $r.

◆ createRoute()

Slim\Router::createRoute (   $methods,
  $pattern,
  $callable 
)
protected

Create a new Route object.

Parameters
string[]$methodsArray of HTTP methods
string$patternThe route pattern
callable$callableThe route callable
Returns
\Slim\Interfaces\RouteInterface

Definition at line 206 of file Router.php.

207 {
208 $route = new Route($methods, $pattern, $callable, $this->routeGroups, $this->routeCounter);
209 if (!empty($this->container)) {
210 $route->setContainer($this->container);
211 }
212
213 return $route;
214 }

◆ dispatch()

Slim\Router::dispatch ( ServerRequestInterface  $request)

Dispatch router for HTTP request.

Parameters
ServerRequestInterface$requestThe current HTTP request object
Returns
array

https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php

Implements Slim\Interfaces\RouterInterface.

Definition at line 187 of file Router.php.

188 {
189 $uri = '/' . ltrim($request->getUri()->getPath(), '/');
190
191 return $this->createDispatcher()->dispatch(
192 $request->getMethod(),
193 $uri
194 );
195 }
createDispatcher()
Definition: Router.php:219

References Psr\Http\Message\RequestInterface\getMethod(), and Psr\Http\Message\RequestInterface\getUri().

+ Here is the call graph for this function:

◆ getNamedRoute()

Slim\Router::getNamedRoute (   $name)

Get named route object.

Parameters
string$nameRoute name
Returns
Route
Exceptions
RuntimeExceptionIf named route does not exist

Implements Slim\Interfaces\RouterInterface.

Definition at line 272 of file Router.php.

273 {
274 foreach ($this->routes as $route) {
275 if ($name == $route->getName()) {
276 return $route;
277 }
278 }
279 throw new RuntimeException('Named route does not exist for name: ' . $name);
280 }
if($format !==null) $name
Definition: metadata.php:146

References $name.

◆ getRoutes()

Slim\Router::getRoutes ( )

Get route objects.

Returns
Route[]

Definition at line 258 of file Router.php.

259 {
260 return $this->routes;
261 }

◆ lookupRoute()

Slim\Router::lookupRoute (   $identifier)
Parameters
$identifier
Returns
\Slim\Interfaces\RouteInterface

Implements Slim\Interfaces\RouterInterface.

Definition at line 341 of file Router.php.

342 {
343 if (!isset($this->routes[$identifier])) {
344 throw new RuntimeException('Route not found, looks like your route cache is stale.');
345 }
346 return $this->routes[$identifier];
347 }

◆ map()

Slim\Router::map (   $methods,
  $pattern,
  $handler 
)

Add route.

Parameters
string[]$methodsArray of HTTP methods
string$patternThe route pattern
callable$handlerThe route callable
Returns
RouteInterface
Exceptions
InvalidArgumentExceptionif the route pattern isn't a string

Implements Slim\Interfaces\RouterInterface.

Definition at line 156 of file Router.php.

157 {
158 if (!is_string($pattern)) {
159 throw new InvalidArgumentException('Route pattern must be a string');
160 }
161
162 // Prepend parent group pattern(s)
163 if ($this->routeGroups) {
164 $pattern = $this->processGroups() . $pattern;
165 }
166
167 // According to RFC methods are defined in uppercase (See RFC 7231)
168 $methods = array_map("strtoupper", $methods);
169
170 // Add route
171 $route = $this->createRoute($methods, $pattern, $handler);
172 $this->routes[$route->getIdentifier()] = $route;
173 $this->routeCounter++;
174
175 return $route;
176 }
processGroups()
Process route groups.
Definition: Router.php:302
createRoute($methods, $pattern, $callable)
Create a new Route object.
Definition: Router.php:206
$handler

References $handler.

◆ pathFor()

Slim\Router::pathFor (   $name,
array  $data = [],
array  $queryParams = [] 
)

Build the path for a named route including the base path.

Parameters
string$nameRoute name
array$dataNamed argument replacement data
array$queryParamsOptional query string parameters
Returns
string
Exceptions
RuntimeExceptionIf named route does not exist
InvalidArgumentExceptionIf required data not provided

Implements Slim\Interfaces\RouterInterface.

Definition at line 425 of file Router.php.

426 {
427 $url = $this->relativePathFor($name, $data, $queryParams);
428
429 if ($this->basePath) {
430 $url = $this->basePath . $url;
431 }
432
433 return $url;
434 }
relativePathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route excluding the base path.
Definition: Router.php:361
$url

References $data, $name, and $url.

◆ popGroup()

Slim\Router::popGroup ( )

Removes the last route group from the array.

Returns
RouteGroup|bool The RouteGroup if successful, else False

Implements Slim\Interfaces\RouterInterface.

Definition at line 331 of file Router.php.

332 {
333 $group = array_pop($this->routeGroups);
334 return $group instanceof RouteGroup ? $group : false;
335 }

◆ processGroups()

Slim\Router::processGroups ( )
protected

Process route groups.

Returns
string A group pattern to prefix routes with

Definition at line 302 of file Router.php.

303 {
304 $pattern = "";
305 foreach ($this->routeGroups as $group) {
306 $pattern .= $group->getPattern();
307 }
308 return $pattern;
309 }

◆ pushGroup()

Slim\Router::pushGroup (   $pattern,
  $callable 
)

Add a route group to the array.

Parameters
string$pattern
callable$callable
Returns
RouteGroupInterface

Implements Slim\Interfaces\RouterInterface.

Definition at line 319 of file Router.php.

320 {
321 $group = new RouteGroup($pattern, $callable);
322 array_push($this->routeGroups, $group);
323 return $group;
324 }

◆ relativePathFor()

Slim\Router::relativePathFor (   $name,
array  $data = [],
array  $queryParams = [] 
)

Build the path for a named route excluding the base path.

Parameters
string$nameRoute name
array$dataNamed argument replacement data
array$queryParamsOptional query string parameters
Returns
string
Exceptions
RuntimeExceptionIf named route does not exist
InvalidArgumentExceptionIf required data not provided

Implements Slim\Interfaces\RouterInterface.

Definition at line 361 of file Router.php.

362 {
363 $route = $this->getNamedRoute($name);
364 $pattern = $route->getPattern();
365
366 $routeDatas = $this->routeParser->parse($pattern);
367 // $routeDatas is an array of all possible routes that can be made. There is
368 // one routedata for each optional parameter plus one for no optional parameters.
369 //
370 // The most specific is last, so we look for that first.
371 $routeDatas = array_reverse($routeDatas);
372
373 $segments = [];
374 foreach ($routeDatas as $routeData) {
375 foreach ($routeData as $item) {
376 if (is_string($item)) {
377 // this segment is a static string
378 $segments[] = $item;
379 continue;
380 }
381
382 // This segment has a parameter: first element is the name
383 if (!array_key_exists($item[0], $data)) {
384 // we don't have a data element for this segment: cancel
385 // testing this routeData item, so that we can try a less
386 // specific routeData item.
387 $segments = [];
388 $segmentName = $item[0];
389 break;
390 }
391 $segments[] = $data[$item[0]];
392 }
393 if (!empty($segments)) {
394 // we found all the parameters for this route data, no need to check
395 // less specific ones
396 break;
397 }
398 }
399
400 if (empty($segments)) {
401 throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName);
402 }
403 $url = implode('', $segments);
404
405 if ($queryParams) {
406 $url .= '?' . http_build_query($queryParams);
407 }
408
409 return $url;
410 }
getNamedRoute($name)
Get named route object.
Definition: Router.php:272

References $data, $name, and $url.

◆ removeNamedRoute()

Slim\Router::removeNamedRoute (   $name)

Remove named route.

Parameters
string$nameRoute name
Exceptions
RuntimeExceptionIf named route does not exist

Definition at line 289 of file Router.php.

290 {
291 $route = $this->getNamedRoute($name);
292
293 // no exception, route exists, now remove by id
294 unset($this->routes[$route->getIdentifier()]);
295 }

References $name.

◆ setBasePath()

Slim\Router::setBasePath (   $basePath)

Set the base path used in pathFor()

Parameters
string$basePath
Returns
self

Definition at line 103 of file Router.php.

104 {
105 if (!is_string($basePath)) {
106 throw new InvalidArgumentException('Router basePath must be a string');
107 }
108
109 $this->basePath = $basePath;
110
111 return $this;
112 }

◆ setCacheFile()

Slim\Router::setCacheFile (   $cacheFile)

Set path to fast route cache file.

If this is false then route caching is disabled.

Parameters
string | false$cacheFile
Returns
self

Definition at line 121 of file Router.php.

122 {
123 if (!is_string($cacheFile) && $cacheFile !== false) {
124 throw new InvalidArgumentException('Router cacheFile must be a string or false');
125 }
126
127 $this->cacheFile = $cacheFile;
128
129 if ($cacheFile !== false && !is_writable(dirname($cacheFile))) {
130 throw new RuntimeException('Router cacheFile directory must be writable');
131 }
132
133
134 return $this;
135 }

◆ setContainer()

Slim\Router::setContainer ( ContainerInterface  $container)
Parameters
ContainerInterface$container

Definition at line 140 of file Router.php.

141 {
142 $this->container = $container;
143 }

References $container.

◆ setDispatcher()

Slim\Router::setDispatcher ( Dispatcher  $dispatcher)
Parameters
\FastRoute\Dispatcher$dispatcher

Definition at line 248 of file Router.php.

249 {
250 $this->dispatcher = $dispatcher;
251 }

◆ urlFor()

Slim\Router::urlFor (   $name,
array  $data = [],
array  $queryParams = [] 
)

Build the path for a named route.

This method is deprecated. Use pathFor() from now on.

Parameters
string$nameRoute name
array$dataNamed argument replacement data
array$queryParamsOptional query string parameters
Returns
string
Exceptions
RuntimeExceptionIf named route does not exist
InvalidArgumentExceptionIf required data not provided

Definition at line 450 of file Router.php.

451 {
452 trigger_error('urlFor() is deprecated. Use pathFor() instead.', E_USER_DEPRECATED);
453 return $this->pathFor($name, $data, $queryParams);
454 }
pathFor($name, array $data=[], array $queryParams=[])
Build the path for a named route including the base path.
Definition: Router.php:425

References $data, and $name.

Field Documentation

◆ $basePath

Slim\Router::$basePath = ''
protected

Definition at line 52 of file Router.php.

◆ $cacheFile

Slim\Router::$cacheFile = false
protected

Definition at line 59 of file Router.php.

◆ $container

Slim\Router::$container
protected

Definition at line 38 of file Router.php.

◆ $dispatcher

Slim\Router::$dispatcher
protected

Definition at line 84 of file Router.php.

◆ $routeCounter

Slim\Router::$routeCounter = 0
protected

Definition at line 72 of file Router.php.

◆ $routeGroups

Slim\Router::$routeGroups = []
protected

Definition at line 79 of file Router.php.

◆ $routeParser

Slim\Router::$routeParser
protected

Definition at line 45 of file Router.php.

◆ $routes

Slim\Router::$routes = []
protected

Definition at line 66 of file Router.php.


The documentation for this class was generated from the following file: