\Cake\RoutingRouteCollection

Contains a collection of routes.

Provides an interface for adding/removing routes and parsing/generating URLs with the routes it contains.

Summary

Methods
Properties
Constants
add()
parse()
parseRequest()
match()
routes()
named()
extensions()
getExtensions()
setExtensions()
registerMiddleware()
middlewareGroup()
hasMiddlewareGroup()
hasMiddleware()
middlewareExists()
applyMiddleware()
getMiddleware()
No public properties found
No constants found
_getNames()
$_routeTable
$_routes
$_named
$_paths
$_middleware
$_middlewareGroups
$_middlewarePaths
$_extensions
N/A
No private methods found
No private properties found
N/A

Properties

$_routeTable

$_routeTable : array

The routes connected to this collection.

Type

array

$_routes

$_routes : array<mixed,\Cake\Routing\Route\Route>

The routes connected to this collection.

Type

array<mixed,\Cake\Routing\Route\Route>

$_named

$_named : array<mixed,\Cake\Routing\Route\Route>

The hash map of named routes that are in this collection.

Type

array<mixed,\Cake\Routing\Route\Route>

$_paths

$_paths : array

Routes indexed by path prefix.

Type

array

$_middleware

$_middleware : array

A map of middleware names and the related objects.

Type

array

$_middlewareGroups

$_middlewareGroups : array

A map of middleware group names and the related middleware names.

Type

array

$_middlewarePaths

$_middlewarePaths : array

A map of paths and the list of applicable middleware.

Type

array

$_extensions

$_extensions : array

Route extensions

Type

array

Methods

add()

add(\Cake\Routing\Route\Route  $route, array  $options = array()) : void

Add a route to the collection.

Parameters

\Cake\Routing\Route\Route $route

The route object to add.

array $options

Additional options for the route. Primarily for the _name option, which enables named routes.

parse()

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

The HTTP method to use.

Throws

\Cake\Routing\Exception\MissingRouteException

When a URL has no matching route.

Returns

array —

An array of request parameters parsed from the URL.

parseRequest()

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.

Throws

\Cake\Routing\Exception\MissingRouteException

When a URL has no matching route.

Returns

array —

An array of request parameters parsed from the URL.

match()

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.

Throws

\Cake\Routing\Exception\MissingRouteException

When no route could be matched.

Returns

string —

The URL string on match.

routes()

routes() : array<mixed,\Cake\Routing\Route\Route>

Get all the connected routes as a flat list.

Returns

array<mixed,\Cake\Routing\Route\Route>

named()

named() : array<mixed,\Cake\Routing\Route\Route>

Get the connected named routes.

Returns

array<mixed,\Cake\Routing\Route\Route>

extensions()

extensions(null|string|array  $extensions = null, boolean  $merge = true) : array

Get/set the extensions that the route collection could handle.

Parameters

null|string|array $extensions

Either the list of extensions to set, or null to get.

boolean $merge

Whether to merge with or override existing extensions. Defaults to true.

Returns

array —

The valid extensions.

getExtensions()

getExtensions() : array

Get the extensions that can be handled.

Returns

array —

The valid extensions.

setExtensions()

setExtensions(array  $extensions, boolean  $merge = true) : $this

Set the extensions that the route collection can handle.

Parameters

array $extensions

The list of extensions to set.

boolean $merge

Whether to merge with or override existing extensions. Defaults to true.

Returns

$this

registerMiddleware()

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

$this

middlewareGroup()

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

$this

hasMiddlewareGroup()

hasMiddlewareGroup(string  $name) : boolean

Check if the named middleware group has been created.

Parameters

string $name

The name of the middleware group to check.

Returns

boolean

hasMiddleware()

hasMiddleware(string  $name) : boolean

Check if the named middleware has been registered.

Parameters

string $name

The name of the middleware to check.

Returns

boolean

middlewareExists()

middlewareExists(string  $name) : boolean

Check if the named middleware or middleware group has been registered.

Parameters

string $name

The name of the middleware to check.

Returns

boolean

applyMiddleware()

applyMiddleware(string  $path, array<mixed,string>  $middleware) : $this

Apply a registered middleware(s) for the provided path

Parameters

string $path

The URL path to register middleware for.

array<mixed,string> $middleware

The middleware names to add for the path.

Returns

$this

getMiddleware()

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

Throws

\RuntimeException

when a requested middleware does not exist.

Returns

array —

An array of middleware. If any of the passed names are groups, the groups middleware will be flattened into the returned list.

_getNames()

_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

array —

The set of names of the url