Router class

Framework\Routing\Router

Class Router.

Implements: JsonSerializable

Defined in libraries/routing/src/Router.php

Properties #

$defaultRouteActionMethod
-
$defaultRouteNotFound
-
$placeholders
-
$collections
-
$matchedCollection
-
$matchedRoute
-
$matchedOrigin
-
$matchedOriginArguments
-
$matchedPath
-
$matchedPathArguments
-
$autoOptions
-
$autoMethods
-
$response
-
$language
-
$debugCollector
-

$defaultRouteActionMethod

protected string $defaultRouteActionMethod = 'index'

$defaultRouteNotFound

protected Closure|string $defaultRouteNotFound

$placeholders

protected static array $placeholders = array ( '{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}' => '([^.]+)', '{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}+)', )

$collections

protected array $collections = array ( )

$matchedCollection

protected ?\Framework\Routing\RouteCollection $matchedCollection = NULL

$matchedRoute

protected ?\Framework\Routing\Route $matchedRoute = NULL

$matchedOrigin

protected ?string $matchedOrigin = NULL

$matchedOriginArguments

protected array $matchedOriginArguments = array ( )

$matchedPath

protected ?string $matchedPath = NULL

$matchedPathArguments

protected array $matchedPathArguments = array ( )

$autoOptions

protected bool $autoOptions = false

$autoMethods

protected bool $autoMethods = false

$response

protected \Framework\HTTP\Response $response

$language

protected \Framework\Language\Language $language

$debugCollector

protected \Framework\Routing\Debug\RoutingCollector $debugCollector

Methods #

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

__construct()

public function __construct(\Framework\HTTP\Response $response, ?\Framework\Language\Language $language = NULL)

Router constructor.

Parameters
  • \Framework\HTTP\Response $response
  • ?\Framework\Language\Language $language

__get()

public function __get(string $property): mixed
Parameters
  • string $property
Returns mixed

addPlaceholder()

public function addPlaceholder(array|string $placeholder, ?string $pattern = NULL): static

Adds Router placeholders.

Parameters
  • array|string $placeholder
  • ?string $pattern
Returns static

fillPlaceholders()

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

Fills argument values into a string with placeholders.

Parameters
  • string $string - The input string
  • string $arguments
Returns string - The string with argument values in place of placeholders
throws
InvalidArgumentException if param not required, empty or invalid
throws
RuntimeException if a pattern position is not found

getCollections()

public function getCollections(): array

Gets all Route Collections.

Returns array

getDebugCollector()

public function getDebugCollector(): ?\Framework\Routing\Debug\RoutingCollector
Returns ?\Framework\Routing\Debug\RoutingCollector

getDefaultRouteActionMethod()

public function getDefaultRouteActionMethod(): string

Gets the default route action method.

Normally, it is "index".

Returns string

getLanguage()

public function getLanguage(): \Framework\Language\Language
Returns \Framework\Language\Language

getMatchedCollection()

public function getMatchedCollection(): ?\Framework\Routing\RouteCollection

Gets the matched Route Collection.

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

Returns ?\Framework\Routing\RouteCollection

getMatchedOrigin()

public function getMatchedOrigin(): ?string

Gets the matched URL Origin.

Returns ?string

getMatchedOriginArguments()

public function getMatchedOriginArguments(): array

Gets the matched URL Origin arguments.

Returns array

getMatchedPath()

public function getMatchedPath(): ?string

Gets the matched URL Path.

Returns ?string

getMatchedPathArguments()

public function getMatchedPathArguments(): array

Gets the matched URL Path arguments.

Returns array

getMatchedRoute()

public function getMatchedRoute(): ?\Framework\Routing\Route

Gets the matched Route.

Returns ?\Framework\Routing\Route

getMatchedUrl()

public function getMatchedUrl(): ?string

Gets the matched URL.

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

Returns ?string

getNamedRoute()

public function getNamedRoute(string $name): \Framework\Routing\Route

Gets a named route.

Parameters
  • string $name
Returns \Framework\Routing\Route
throws
RuntimeException if named route not found

getPlaceholders()

public function getPlaceholders(): array

Gets all Router placeholders.

Returns array

getResponse()

public function getResponse(): \Framework\HTTP\Response

Gets the HTTP Response instance.

Returns \Framework\HTTP\Response

getRouteNotFound()

public function getRouteNotFound(): \Framework\Routing\Route

Gets the Route Not Found.

Must be called after {@see Router::match()} and will return the Route
Not Found from the matched collection or the Default Route Not Found
from the router.

Returns \Framework\Routing\Route

getRoutes()

public function getRoutes(): array

Gets all routes, except the not found.

Returns array - The HTTP Methods as keys and its Routes as values

hasNamedRoute()

public function hasNamedRoute(string $name): bool

Tells if it has a named route.

Parameters
  • string $name
Returns bool

isAutoMethods()

public function isAutoMethods(): bool

Tells if auto methods is enabled.

Returns bool

isAutoOptions()

public function isAutoOptions(): bool

Tells if auto options is enabled.

Returns bool

jsonSerialize()

public function jsonSerialize(): array
Returns array

match()

public function match(): \Framework\Routing\Route

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

Returns \Framework\Routing\Route - Always returns a Route, even if it is the Route Not Found

replacePlaceholders()

public function replacePlaceholders(string $string, bool $flip = false): string

Replaces string placeholders with patterns or patterns with placeholders.

Parameters
  • string $string - The string with placeholders or patterns
  • bool $flip - Set true to replace patterns with placeholders
Returns string

serve()

public function serve(?string $origin, callable $callable, ?string $collectionName = NULL): static

Serves a RouteCollection to a specific Origin.

Parameters
  • ?string $origin - 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
  • ?string $collectionName - The RouteCollection name
Returns static

setAutoMethods()

public function setAutoMethods(bool $enabled = true): 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.

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

Parameters
  • bool $enabled - true to enable, false to disable
Returns static

setAutoOptions()

public function setAutoOptions(bool $enabled = true): static

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

Parameters
  • bool $enabled - true to enable, false to disable
Returns static

setDebugCollector()

public function setDebugCollector(\Framework\Routing\Debug\RoutingCollector $debugCollector): static
Parameters
  • \Framework\Routing\Debug\RoutingCollector $debugCollector
Returns static

setDefaultRouteActionMethod()

public function setDefaultRouteActionMethod(string $action): static

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

Parameters
  • string $action
Returns static

setDefaultRouteNotFound()

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

Sets the Default Route Not Found action.

Parameters
  • Closure|string $action - the function to run when no Route path is found
Returns static

setLanguage()

public function setLanguage(?\Framework\Language\Language $language = NULL): static
Parameters
  • ?\Framework\Language\Language $language
Returns static

addCollection()

protected function addCollection(\Framework\Routing\RouteCollection $collection): static
Parameters
  • \Framework\Routing\RouteCollection $collection
Returns static

addServedCollection()

protected function addServedCollection(?string $origin, callable $callable, ?string $collectionName = NULL): static
Parameters
  • ?string $origin
  • callable $callable
  • ?string $collectionName
Returns static

getAllowedMethods()

protected function getAllowedMethods(\Framework\Routing\RouteCollection $collection): array
Parameters
  • \Framework\Routing\RouteCollection $collection
Returns array

getAlternativeRoute()

protected function getAlternativeRoute(string $method, \Framework\Routing\RouteCollection $collection): \Framework\Routing\Route
Parameters
  • string $method
  • \Framework\Routing\RouteCollection $collection
Returns \Framework\Routing\Route

getDefaultRouteNotFound()

protected function getDefaultRouteNotFound(): \Framework\Routing\Route
Returns \Framework\Routing\Route

getRouteWithAllowHeader()

protected function getRouteWithAllowHeader(\Framework\Routing\RouteCollection $collection, int $code): ?\Framework\Routing\Route
Parameters
  • \Framework\Routing\RouteCollection $collection
  • int $code
Returns ?\Framework\Routing\Route

makeMatchedRoute()

protected function makeMatchedRoute(): \Framework\Routing\Route
Returns \Framework\Routing\Route

makePath()

protected function makePath(string $path): string

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

Parameters
  • string $path
Returns string

matchCollection()

protected function matchCollection(string $origin): ?\Framework\Routing\RouteCollection
Parameters
  • string $origin
Returns ?\Framework\Routing\RouteCollection

matchRoute()

protected function matchRoute(string $method, \Framework\Routing\RouteCollection $collection, string $path): ?\Framework\Routing\Route
Parameters
  • string $method
  • \Framework\Routing\RouteCollection $collection
  • string $path
Returns ?\Framework\Routing\Route

setMatchedCollection()

protected function setMatchedCollection(\Framework\Routing\RouteCollection $matchedCollection): static
Parameters
  • \Framework\Routing\RouteCollection $matchedCollection
Returns static

setMatchedOrigin()

protected function setMatchedOrigin(string $origin): static
Parameters
  • string $origin
Returns static

setMatchedOriginArguments()

protected function setMatchedOriginArguments(array $arguments): static
Parameters
  • array $arguments
Returns static

setMatchedPath()

protected function setMatchedPath(string $path): static
Parameters
  • string $path
Returns static

setMatchedPathArguments()

protected function setMatchedPathArguments(array $arguments): static
Parameters
  • array $arguments
Returns static

setMatchedRoute()

protected function setMatchedRoute(\Framework\Routing\Route $route): static
Parameters
  • \Framework\Routing\Route $route
Returns static