Fr3nch13/CakePHP Utilities

Route
in package

A single Route used by the Router to connect requests to parameter maps.

Not normally created as a standalone. Use Router::connect() to create Routes for your application.

Table of Contents

Constants

VALID_METHODS  = ['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']
Valid HTTP methods.
PLACEHOLDER_REGEX  = '#\{([a-z][a-z0-9-_]*)\}#i'
Regex for matching braced placholders in route template.

Properties

$defaults  : array<string|int, mixed>
Default parameters for a Route
$keys  : array<string|int, mixed>
An array of named segments in a Route.
$options  : array<string|int, mixed>
An array of additional parameters for the Route.
$template  : string
The routes template string.
$_compiledRoute  : string|null
The compiled route regular expression
$_extensions  : array<string|int, string>
List of connected extensions for this route.
$_greedy  : bool
Is this route a greedy route? Greedy routes have a `/*` in their template
$_name  : string|null
The name for a route. Fetch with Route::getName();
$braceKeys  : bool
Track whether brace keys `{var}` were used.
$middleware  : array<string|int, mixed>
List of middleware that should be applied.

Methods

__construct()  : mixed
Constructor for a Route
__set_state()  : static
Set state magic method to support var_export
compile()  : string
Compiles the route's regular expression.
compiled()  : bool
Check if a Route has been compiled into a regular expression.
getExtensions()  : array<string|int, string>
Get the supported extensions for this route.
getMiddleware()  : array<string|int, mixed>
Get the names of the middleware that should be applied to this route.
getName()  : string
Get the standardized plugin.controller:action name for a route.
hostMatches()  : bool
Check to see if the host matches the route requirements
match()  : string|null
Check if a URL array matches this route instance.
parse()  : array<string|int, mixed>|null
Checks to see if the given URL can be parsed by this route.
parseRequest()  : array<string|int, mixed>|null
Checks to see if the given URL can be parsed by this route.
setExtensions()  : $this
Set the supported extensions for this route.
setHost()  : $this
Set host requirement
setMethods()  : $this
Set the accepted HTTP methods for this route.
setMiddleware()  : $this
Set the names of the middleware that should be applied to this route.
setPass()  : $this
Set the names of parameters that will be converted into passed parameters
setPatterns()  : $this
Set regexp patterns for routing parameters
setPersist()  : $this
Set the names of parameters that will persisted automatically
staticPath()  : string
Get the static path portion for this route.
_matchMethod()  : bool
Check whether the URL's HTTP method matches.
_parseArgs()  : array<string|int, string>
Parse passed parameters into a list of passed args.
_parseExtension()  : array<string|int, mixed>
Removes the extension from $url if it contains a registered extension.
_persistParams()  : array<string|int, mixed>
Apply persistent parameters to a URL array. Persistent parameters are a special key used during route creation to force route parameters to persist when omitted from a URL array.
_writeRoute()  : void
Builds a route regular expression.
_writeUrl()  : string
Converts a matching route array into a URL string.
normalizeAndValidateMethods()  : array<string|int, string>|string
Normalize method names to upper case and validate that they are valid HTTP methods.

Constants

VALID_METHODS

Valid HTTP methods.

public array<string|int, string> VALID_METHODS = ['GET', 'PUT', 'POST', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']

PLACEHOLDER_REGEX

Regex for matching braced placholders in route template.

protected string PLACEHOLDER_REGEX = '#\{([a-z][a-z0-9-_]*)\}#i'

Properties

$defaults

Default parameters for a Route

public array<string|int, mixed> $defaults = []

$keys

An array of named segments in a Route.

public array<string|int, mixed> $keys = []

/{controller}/{action}/{id} has 3 key elements

$options

An array of additional parameters for the Route.

public array<string|int, mixed> $options = []

$template

The routes template string.

public string $template

$_compiledRoute

The compiled route regular expression

protected string|null $_compiledRoute

$_extensions

List of connected extensions for this route.

protected array<string|int, string> $_extensions = []

$_greedy

Is this route a greedy route? Greedy routes have a `/*` in their template

protected bool $_greedy = false

$_name

The name for a route. Fetch with Route::getName();

protected string|null $_name

$braceKeys

Track whether brace keys `{var}` were used.

protected bool $braceKeys = true

$middleware

List of middleware that should be applied.

protected array<string|int, mixed> $middleware = []

Methods

__construct()

Constructor for a Route

public __construct(string $template[, array<string|int, mixed> $defaults = [] ][, array<string, mixed> $options = [] ]) : mixed

Options

  • _ext - Defines the extensions used for this route.
  • _middleware - Define the middleware names for this route.
  • pass - Copies the listed parameters into params['pass'].
  • _method - Defines the HTTP method(s) the route applies to. It can be a string or array of valid HTTP method name.
  • _host - Define the host name pattern if you want this route to only match specific host names. You can use .* and to create wildcard subdomains/hosts e.g. *.example.com matches all subdomains on example.com.
  • '_port` - Define the port if you want this route to only match specific port number.
  • '_urldecode' - Set to false to disable URL decoding before route parsing.
Parameters
$template : string

Template string with parameter placeholders

$defaults : array<string|int, mixed> = []

Defaults for the route.

$options : array<string, mixed> = []

Array of additional options for the Route

Tags
throws
InvalidArgumentException

When $options['_method'] are not in VALID_METHODS list.

__set_state()

Set state magic method to support var_export

public static __set_state(array<string, mixed> $fields) : static

This method helps for applications that want to implement router caching.

Parameters
$fields : array<string, mixed>

Key/Value of object attributes

Return values
static

A new instance of the route

compile()

Compiles the route's regular expression.

public compile() : string

Modifies defaults property so all necessary keys are set and populates $this->names with the named routing elements.

Return values
string

Returns a string regular expression of the compiled route.

compiled()

Check if a Route has been compiled into a regular expression.

public compiled() : bool
Return values
bool

getExtensions()

Get the supported extensions for this route.

public getExtensions() : array<string|int, string>
Return values
array<string|int, string>

getMiddleware()

Get the names of the middleware that should be applied to this route.

public getMiddleware() : array<string|int, mixed>
Return values
array<string|int, mixed>

getName()

Get the standardized plugin.controller:action name for a route.

public getName() : string
Return values
string

hostMatches()

Check to see if the host matches the route requirements

public hostMatches(string $host) : bool
Parameters
$host : string

The request's host name

Return values
bool

Whether the host matches any conditions set in for this route.

match()

Check if a URL array matches this route instance.

public match(array<string|int, mixed> $url[, array<string|int, mixed> $context = [] ]) : string|null

If the URL matches the route parameters and settings, then return a generated string URL. If the URL doesn't match the route parameters, false will be returned. This method handles the reverse routing or conversion of URL arrays into string URLs.

Parameters
$url : array<string|int, mixed>

An array of parameters to check matching with.

$context : array<string|int, mixed> = []

An array of the current request context. Contains information such as the current host, scheme, port, base directory and other url params.

Return values
string|null

Either a string URL for the parameters if they match or null.

parse()

Checks to see if the given URL can be parsed by this route.

public parse(string $url, string $method) : array<string|int, mixed>|null

If the route can be parsed an array of parameters will be returned; if not null will be returned. String URLs are parsed if they match a routes regular expression.

Parameters
$url : string

The URL to attempt to parse.

$method : string

The HTTP method of the request being parsed.

Tags
throws
BadRequestException

When method is not an empty string and not in VALID_METHODS list.

Return values
array<string|int, mixed>|null

An array of request parameters, or null on failure.

parseRequest()

Checks to see if the given URL can be parsed by this route.

public parseRequest(ServerRequestInterface $request) : array<string|int, mixed>|null

If the route can be parsed an array of parameters will be returned; if not null will be returned.

Parameters
$request : ServerRequestInterface

The URL to attempt to parse.

Return values
array<string|int, mixed>|null

An array of request parameters, or null on failure.

setExtensions()

Set the supported extensions for this route.

public setExtensions(array<string|int, string> $extensions) : $this
Parameters
$extensions : array<string|int, string>

The extensions to set.

Return values
$this

setHost()

Set host requirement

public setHost(string $host) : $this
Parameters
$host : string

The host name this route is bound to

Return values
$this

setMethods()

Set the accepted HTTP methods for this route.

public setMethods(array<string|int, string> $methods) : $this
Parameters
$methods : array<string|int, string>

The HTTP methods to accept.

Tags
throws
InvalidArgumentException

When methods are not in VALID_METHODS list.

Return values
$this

setMiddleware()

Set the names of the middleware that should be applied to this route.

public setMiddleware(array<string|int, mixed> $middleware) : $this
Parameters
$middleware : array<string|int, mixed>

The list of middleware names to apply to this route. Middleware names will not be checked until the route is matched.

Return values
$this

setPass()

Set the names of parameters that will be converted into passed parameters

public setPass(array<string|int, string> $names) : $this
Parameters
$names : array<string|int, string>

The names of the parameters that should be passed.

Return values
$this

setPatterns()

Set regexp patterns for routing parameters

public setPatterns(array<string|int, string> $patterns) : $this

If any of your patterns contain multibyte values, the multibytePattern mode will be enabled.

Parameters
$patterns : array<string|int, string>

The patterns to apply to routing elements

Return values
$this

setPersist()

Set the names of parameters that will persisted automatically

public setPersist(array<string|int, mixed> $names) : $this

Persistent parameters allow you to define which route parameters should be automatically included when generating new URLs. You can override persistent parameters by redefining them in a URL or remove them by setting the persistent parameter to false.

// remove a persistent 'date' parameter
Router::url(['date' => false', ...]);
Parameters
$names : array<string|int, mixed>

The names of the parameters that should be passed.

Return values
$this

staticPath()

Get the static path portion for this route.

public staticPath() : string
Return values
string

_matchMethod()

Check whether the URL's HTTP method matches.

protected _matchMethod(array<string|int, mixed> $url) : bool
Parameters
$url : array<string|int, mixed>

The array for the URL being generated.

Return values
bool

_parseArgs()

Parse passed parameters into a list of passed args.

protected _parseArgs(string $args, array<string|int, mixed> $context) : array<string|int, string>

Return true if a given named $param's $val matches a given $rule depending on $context. Currently implemented rule types are controller, action and match that can be combined with each other.

Parameters
$args : string

A string with the passed params. eg. /1/foo

$context : array<string|int, mixed>

The current route context, which should contain controller/action keys.

Return values
array<string|int, string>

Array of passed args.

_parseExtension()

Removes the extension from $url if it contains a registered extension.

protected _parseExtension(string $url) : array<string|int, mixed>

If no registered extension is found, no extension is returned and the URL is returned unmodified.

Parameters
$url : string

The url to parse.

Return values
array<string|int, mixed>

containing url, extension

_persistParams()

Apply persistent parameters to a URL array. Persistent parameters are a special key used during route creation to force route parameters to persist when omitted from a URL array.

protected _persistParams(array<string|int, mixed> $url, array<string|int, mixed> $params) : array<string|int, mixed>
Parameters
$url : array<string|int, mixed>

The array to apply persistent parameters to.

$params : array<string|int, mixed>

An array of persistent values to replace persistent ones.

Return values
array<string|int, mixed>

An array with persistent parameters applied.

_writeRoute()

Builds a route regular expression.

protected _writeRoute() : void

Uses the template, defaults and options properties to compile a regular expression that can be used to parse request strings.

_writeUrl()

Converts a matching route array into a URL string.

protected _writeUrl(array<string|int, mixed> $params[, array<string|int, mixed> $pass = [] ][, array<string|int, mixed> $query = [] ]) : string

Composes the string URL using the template used to create the route.

Parameters
$params : array<string|int, mixed>

The params to convert to a string url

$pass : array<string|int, mixed> = []

The additional passed arguments

$query : array<string|int, mixed> = []

An array of parameters

Return values
string

Composed route string.

normalizeAndValidateMethods()

Normalize method names to upper case and validate that they are valid HTTP methods.

protected normalizeAndValidateMethods(array<string|int, string>|string $methods) : array<string|int, string>|string
Parameters
$methods : array<string|int, string>|string

Methods.

Tags
throws
InvalidArgumentException

When methods are not in VALID_METHODS list.

Return values
array<string|int, string>|string

        
On this page

Search results