Aplus Framework Docs

Router
in package
implements JsonSerializable

Class Router.

Interfaces, Classes, Traits and Enums

JsonSerializable

Table of Contents

$autoMethods  : bool
$autoOptions  : bool
$collections  : array<string|int, mixed>
$debugCollector  : RoutingCollector
$defaultRouteActionMethod  : string
$defaultRouteNotFound  : Closure|string
$language  : Language
$matchedCollection  : RouteCollection|null
$matchedOrigin  : string|null
$matchedOriginArguments  : array<string|int, mixed>
$matchedPath  : string|null
$matchedPathArguments  : array<string|int, mixed>
$matchedRoute  : Route|null
$placeholders  : array<string|int, mixed>
$response  : Response
__construct()  : mixed
Router constructor.
__get()  : mixed
addPlaceholder()  : static
Adds Router placeholders.
fillPlaceholders()  : string
Fills argument values into a string with placeholders.
getCollections()  : array<int, RouteCollection>
Gets all Route Collections.
getDebugCollector()  : RoutingCollector|null
getDefaultRouteActionMethod()  : string
Gets the default route action method.
getLanguage()  : Language
getMatchedCollection()  : RouteCollection|null
Gets the matched Route Collection.
getMatchedOrigin()  : string|null
Gets the matched URL Origin.
getMatchedOriginArguments()  : array<int, string>
Gets the matched URL Origin arguments.
getMatchedPath()  : string|null
Gets the matched URL Path.
getMatchedPathArguments()  : array<int, string>
Gets the matched URL Path arguments.
getMatchedRoute()  : Route|null
Gets the matched Route.
getMatchedUrl()  : string|null
Gets the matched URL.
getNamedRoute()  : Route
Gets a named route.
getPlaceholders()  : array<string, string>
Gets all Router placeholders.
getResponse()  : Response
Gets the HTTP Response instance.
getRouteNotFound()  : Route
Gets the Route Not Found.
getRoutes()  : array<string, array<string|int, Route>>
Gets all routes, except the not found.
hasNamedRoute()  : bool
Tells if it has a named route.
isAutoMethods()  : bool
Tells if auto methods is enabled.
isAutoOptions()  : bool
Tells if auto options is enabled.
jsonSerialize()  : array<string, mixed>
match()  : Route
Match HTTP Method and URL against RouteCollections to process the request.
replacePlaceholders()  : string
Replaces string placeholders with patterns or patterns with placeholders.
serve()  : static
Serves a RouteCollection to a specific Origin.
setAutoMethods()  : static
Enable/disable the feature of auto-detect and show HTTP allowed methods via the Allow header when a route with the requested method does not exist.
setAutoOptions()  : static
Enable/disable the feature of auto-detect and show HTTP allowed methods via the Allow header when the Request has the OPTIONS method.
setDebugCollector()  : static
setDefaultRouteActionMethod()  : static
Set the class method name to be called when a Route action is set without a method.
setDefaultRouteNotFound()  : static
Sets the Default Route Not Found action.
setLanguage()  : static
addCollection()  : static
addServedCollection()  : static
getAllowedMethods()  : array<int, string>
getAlternativeRoute()  : Route
getDefaultRouteNotFound()  : Route
getRouteWithAllowHeader()  : Route|null
makeMatchedRoute()  : Route
makePath()  : string
Creates a path without a trailing slash to be able to match both with and without a slash at the end.
matchCollection()  : RouteCollection|null
matchRoute()  : Route|null
setMatchedCollection()  : static
setMatchedOrigin()  : static
setMatchedOriginArguments()  : static
setMatchedPath()  : static
setMatchedPathArguments()  : static
setMatchedRoute()  : static

Properties

$autoMethods

protected bool $autoMethods = false

$autoOptions

protected bool $autoOptions = false

$collections

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

$defaultRouteActionMethod

protected string $defaultRouteActionMethod = 'index'

$defaultRouteNotFound

protected Closure|string $defaultRouteNotFound

$matchedOrigin

protected string|null $matchedOrigin = null

$matchedOriginArguments

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

$matchedPath

protected string|null $matchedPath = null

$matchedPathArguments

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

$placeholders

protected static array<string|int, mixed> $placeholders = [ '{alpha}' => '([a-zA-Z]+)', '{alphanum}' => '([a-zA-Z0-9]+)', '{any}' => '(.*)', '{hex}' => '([[:xdigit:]]+)', '{int}' => '([0-9]{1,18}+)', '{md5}' => '([a-f0-9]{32}+)', '{num}' => '([0-9]+)', '{port}' => '([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])', '{scheme}' => '(https?)', '{segment}' => '([^/]+)', '{slug}' => '([a-z0-9_-]+)', '{subdomain}' => '([^.]+)', //'{subdomain}' => '([A-Za-z0-9](?:[a-zA-Z0-9\-]{0,61}[A-Za-z0-9])?)', '{title}' => '([a-zA-Z0-9_-]+)', '{uuid}' => '([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}+)', ]

$response

protected Response $response

Methods

__construct()

Router constructor.

public __construct(Response $response[, Language|null $language = null ]) : mixed
Parameters
$response : Response
$language : Language|null = null
Return values
mixed

__get()

public __get(string $property) : mixed
Parameters
$property : string
Return values
mixed

addPlaceholder()

Adds Router placeholders.

public addPlaceholder(array<string, string>|string $placeholder[, string|null $pattern = null ]) : static
Parameters
$placeholder : array<string, string>|string
$pattern : string|null = null
Return values
static

fillPlaceholders()

Fills argument values into a string with placeholders.

public fillPlaceholders(string $string, string ...$arguments) : string
Parameters
$string : string

The input string

$arguments : string

Values to fill the string placeholders

Tags
throws
InvalidArgumentException

if param not required, empty or invalid

throws
RuntimeException

if a pattern position is not found

Return values
string

The string with argument values in place of placeholders

getMatchedCollection()

Gets the matched Route Collection.

public getMatchedCollection() : RouteCollection|null

Note: Will return null if no URL Origin was matched in a Route Collection

Return values
RouteCollection|null

getMatchedOrigin()

Gets the matched URL Origin.

public getMatchedOrigin() : string|null
Return values
string|null

getMatchedOriginArguments()

Gets the matched URL Origin arguments.

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

getMatchedPath()

Gets the matched URL Path.

public getMatchedPath() : string|null
Return values
string|null

getMatchedPathArguments()

Gets the matched URL Path arguments.

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

getMatchedRoute()

Gets the matched Route.

public getMatchedRoute() : Route|null
Return values
Route|null

getMatchedUrl()

Gets the matched URL.

public getMatchedUrl() : string|null

Note: This method does not return the URL query. If it is needed, get with Request::getUrl().

Return values
string|null

getNamedRoute()

Gets a named route.

public getNamedRoute(string $name) : Route
Parameters
$name : string
Tags
throws
RuntimeException

if named route not found

Return values
Route

getPlaceholders()

Gets all Router placeholders.

public getPlaceholders() : array<string, string>
Return values
array<string, string>

getResponse()

Gets the HTTP Response instance.

public getResponse() : Response
Return values
Response

getRoutes()

Gets all routes, except the not found.

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

The HTTP Methods as keys and its Routes as values

hasNamedRoute()

Tells if it has a named route.

public hasNamedRoute(string $name) : bool
Parameters
$name : string
Return values
bool

jsonSerialize()

public jsonSerialize() : array<string, mixed>
Return values
array<string, mixed>

match()

Match HTTP Method and URL against RouteCollections to process the request.

public match() : Route
Tags
see
Router::serve()
Return values
Route

Always returns a Route, even if it is the Route Not Found

replacePlaceholders()

Replaces string placeholders with patterns or patterns with placeholders.

public replacePlaceholders(string $string[, bool $flip = false ]) : string
Parameters
$string : string

The string with placeholders or patterns

$flip : bool = false

Set true to replace patterns with placeholders

Return values
string

serve()

Serves a RouteCollection to a specific Origin.

public serve(string|null $origin, callable $callable[, string|null $collectionName = null ]) : static
Parameters
$origin : string|null

URL Origin. A string in the following format: {scheme}://{hostname}[:{port}]. Null to auto-detect.

$callable : callable

A function receiving an instance of RouteCollection as the first parameter

$collectionName : string|null = null

The RouteCollection name

Return values
static

setAutoMethods()

Enable/disable the feature of auto-detect and show HTTP allowed methods via the Allow header when a route with the requested method does not exist.

public setAutoMethods([bool $enabled = true ]) : static

A response with code 405 "Method Not Allowed" will trigger.

Parameters
$enabled : bool = true

true to enable, false to disable

Tags
see
Status::METHOD_NOT_ALLOWED
see
ResponseHeader::ALLOW
Return values
static

setAutoOptions()

Enable/disable the feature of auto-detect and show HTTP allowed methods via the Allow header when the Request has the OPTIONS method.

public setAutoOptions([bool $enabled = true ]) : static
Parameters
$enabled : bool = true

true to enable, false to disable

Tags
see
Method::OPTIONS
see
ResponseHeader::ALLOW
Return values
static

setDefaultRouteActionMethod()

Set the class method name to be called when a Route action is set without a method.

public setDefaultRouteActionMethod(string $action) : static
Parameters
$action : string
Return values
static

setDefaultRouteNotFound()

Sets the Default Route Not Found action.

public setDefaultRouteNotFound(Closure|string $action) : static
Parameters
$action : Closure|string

the function to run when no Route path is found

Return values
static

setLanguage()

public setLanguage([Language|null $language = null ]) : static
Parameters
$language : Language|null = null
Return values
static

addServedCollection()

protected addServedCollection(string|null $origin, callable $callable[, string|null $collectionName = null ]) : static
Parameters
$origin : string|null
$callable : callable
$collectionName : string|null = null
Return values
static

getDefaultRouteNotFound()

protected getDefaultRouteNotFound() : Route
Return values
Route

makePath()

Creates a path without a trailing slash to be able to match both with and without a slash at the end.

protected makePath(string $path) : string
Parameters
$path : string
Tags
since
3.4.3
Return values
string

setMatchedOrigin()

protected setMatchedOrigin(string $origin) : static
Parameters
$origin : string
Return values
static

setMatchedOriginArguments()

protected setMatchedOriginArguments(array<int, string> $arguments) : static
Parameters
$arguments : array<int, string>
Return values
static

setMatchedPath()

protected setMatchedPath(string $path) : static
Parameters
$path : string
Return values
static

setMatchedPathArguments()

protected setMatchedPathArguments(array<int, string> $arguments) : static
Parameters
$arguments : array<int, string>
Return values
static

setMatchedRoute()

protected setMatchedRoute(Route $route) : static
Parameters
$route : Route
Return values
static

Search results