Class RouteCollection
Contains a collection of routes.
Provides an interface for adding/removing routes and parsing/generating URLs with the routes it contains.
Property Summary
-
$_extensions protected
arrayRoute extensions
-
$_middleware protected
arrayA map of middleware names and the related objects.
-
$_middlewareGroups protected
arrayA map of middleware group names and the related middleware names.
-
$_middlewarePaths protected
arrayA map of paths and the list of applicable middleware.
-
$_named protected
Cake\Routing\Route\Route[]The hash map of named routes that are in this collection.
-
$_paths protected
arrayRoutes indexed by path prefix.
-
$_routeTable protected
arrayThe routes connected to this collection.
-
$_routes protected
Cake\Routing\Route\Route[]The routes connected to this collection.
Method Summary
-
_getNames() protected
Get the set of names from the $url. Accepts both older style array urls, and newer style urls containing '_name'
-
add() public
Add a route to the collection.
-
applyMiddleware() public
Apply a registered middleware(s) for the provided path
-
extensions() public deprecated
Get/set the extensions that the route collection could handle.
-
getExtensions() public
Get the extensions that can be handled.
-
getMiddleware() public
Get an array of middleware given a list of names
-
hasMiddleware() public
Check if the named middleware has been registered.
-
hasMiddlewareGroup() public
Check if the named middleware group has been created.
-
match() public
Reverse route or match a $url array with the connected routes.
-
middlewareExists() public
Check if the named middleware or middleware group has been registered.
-
middlewareGroup() public
Add middleware to a middleware group
-
named() public
Get the connected named routes.
-
parse() public
Takes the URL string and iterates the routes until one is able to parse the route.
-
parseRequest() public
Takes the ServerRequestInterface, iterates the routes until one is able to parse the route.
-
registerMiddleware() public
Register a middleware with the RouteCollection.
-
routes() public
Get all the connected routes as a flat list.
-
setExtensions() public
Set the extensions that the route collection can handle.
Method Detail
_getNames() ¶ protected
_getNames(array $url): array
Get the set of names from the $url. Accepts both older style array urls, and newer style urls containing '_name'
Parameters
-
array$url The url to match.
Returns
arrayThe set of names of the url
add() ¶ public
add(Cake\Routing\Route\Route $route, array $options = []): void
Add a route to the collection.
Parameters
-
Cake\Routing\Route\Route$route The route object to add.
-
array$options optional Additional options for the route. Primarily for the
_nameoption, which enables named routes.
Returns
voidapplyMiddleware() ¶ public
applyMiddleware(string $path, string[] $middleware): $this
Apply a registered middleware(s) for the provided path
Parameters
-
string$path The URL path to register middleware for.
-
string[]$middleware The middleware names to add for the path.
Returns
$thisextensions() ¶ public
extensions(null|string|array $extensions = null, bool $merge = true): array
Get/set the extensions that the route collection could handle.
Parameters
-
null|string|array$extensions optional Either the list of extensions to set, or null to get.
-
bool$merge optional Whether to merge with or override existing extensions. Defaults to
true.
Returns
arrayThe valid extensions.
getExtensions() ¶ public
getExtensions(): array
Get the extensions that can be handled.
Returns
arrayThe valid extensions.
getMiddleware() ¶ public
getMiddleware(array $names): array
Get an array of middleware given a list of names
Parameters
-
array$names The names of the middleware or groups to fetch
Returns
arrayAn array of middleware. If any of the passed names are groups, the groups middleware will be flattened into the returned list.
Throws
RuntimeExceptionwhen a requested middleware does not exist.
hasMiddleware() ¶ public
hasMiddleware(string $name): bool
Check if the named middleware has been registered.
Parameters
-
string$name The name of the middleware to check.
Returns
boolhasMiddlewareGroup() ¶ public
hasMiddlewareGroup(string $name): bool
Check if the named middleware group has been created.
Parameters
-
string$name The name of the middleware group to check.
Returns
boolmatch() ¶ public
match(array $url, array $context): string
Reverse route or match a $url array with the connected routes.
Returns either the URL string generated by the route, or throws an exception on failure.
Parameters
-
array$url The URL to match.
-
array$context The request context to use. Contains _base, _port, _host, _scheme and params keys.
Returns
stringThe URL string on match.
Throws
Cake\Routing\Exception\MissingRouteExceptionWhen no route could be matched.
middlewareExists() ¶ public
middlewareExists(string $name): bool
Check if the named middleware or middleware group has been registered.
Parameters
-
string$name The name of the middleware to check.
Returns
boolmiddlewareGroup() ¶ public
middlewareGroup(string $name, array $middlewareNames): $this
Add middleware to a middleware group
Parameters
-
string$name Name of the middleware group
-
array$middlewareNames Names of the middleware
Returns
$thisnamed() ¶ public
named(): Cake\Routing\Route\Route[]
Get the connected named routes.
Returns
Cake\Routing\Route\Route[]parse() ¶ public
parse(string $url, string $method = ''): array
Takes the URL string and iterates the routes until one is able to parse the route.
Parameters
-
string$url URL to parse.
-
string$method optional The HTTP method to use.
Returns
arrayAn array of request parameters parsed from the URL.
Throws
Cake\Routing\Exception\MissingRouteExceptionWhen a URL has no matching route.
parseRequest() ¶ public
parseRequest(Psr\Http\Message\ServerRequestInterface $request): array
Takes the ServerRequestInterface, iterates the routes until one is able to parse the route.
Parameters
-
Psr\Http\Message\ServerRequestInterface$request The request to parse route data from.
Returns
arrayAn array of request parameters parsed from the URL.
Throws
Cake\Routing\Exception\MissingRouteExceptionWhen a URL has no matching route.
registerMiddleware() ¶ public
registerMiddleware(string $name, callable|string $middleware): $this
Register a middleware with the RouteCollection.
Once middleware has been registered, it can be applied to the current routing scope or any child scopes that share the same RouteCollection.
Parameters
-
string$name The name of the middleware. Used when applying middleware to a scope.
-
callable|string$middleware The middleware callable or class name to register.
Returns
$thisroutes() ¶ public
routes(): Cake\Routing\Route\Route[]
Get all the connected routes as a flat list.
Returns
Cake\Routing\Route\Route[]setExtensions() ¶ public
setExtensions(array $extensions, bool $merge = true): $this
Set the extensions that the route collection can handle.
Parameters
-
array$extensions The list of extensions to set.
-
bool$merge optional Whether to merge with or override existing extensions. Defaults to
true.
Returns
$this