final class Router (View source)

Collects immutable route definitions and resolves requests after registration freezes.

It owns route-name uniqueness and path normalization but leaves request preprocessing to the dispatch boundary.

Methods

Initializes an empty mutable route collection.

get(string $path, ControllerAction $action)

Registers a GET route using the current group prefix.

post(string $path, ControllerAction $action)

Registers a POST route using the current group prefix.

put(string $path, ControllerAction $action)

Registers a PUT route using the current group prefix.

patch(string $path, ControllerAction $action)

Registers a PATCH route using the current group prefix.

delete(string $path, ControllerAction $action)

Registers a DELETE route using the current group prefix.

head(string $path, ControllerAction $action)

Registers a HEAD route using the current group prefix.

options(string $path, ControllerAction $action)

Registers an OPTIONS route using the current group prefix.

getNamed(string $name, string $path, ControllerAction $action)

Registers a named GET route and reserves its URL-generation name.

postNamed(string $name, string $path, ControllerAction $action)

Registers a named POST route and reserves its URL-generation name.

putNamed(string $name, string $path, ControllerAction $action)

Registers a named PUT route and reserves its URL-generation name.

patchNamed(string $name, string $path, ControllerAction $action)

Registers a named PATCH route and reserves its URL-generation name.

deleteNamed(string $name, string $path, ControllerAction $action)

Registers a named DELETE route and reserves its URL-generation name.

headNamed(string $name, string $path, ControllerAction $action)

Registers a named HEAD route and reserves its URL-generation name.

optionsNamed(string $name, string $path, ControllerAction $action)

Registers a named OPTIONS route and reserves its URL-generation name.

map(HttpMethod|string $method, string $path, ControllerAction $action)

Adds one route definition and applies constraints configured for localized groups.

mapNamed(string $name, HttpMethod|string $method, string $path, ControllerAction $action)

Adds one named route after rejecting a duplicate effective name.

group(string $prefix, callable $builder)

Runs a registration callback with a temporary path prefix and returns its routes.

localizedGroup(callable $builder)

Registers parallel plain and locale-prefixed variants for the callback's routes.

void
configureLocalizedRoutes(string $routeNamePrefix = 'localized.', string|null $routePrefix = null, string $localeParameter = 'locale', array $supportedLocales = [])

Configures the static locale-prefix convention used by future localized groups.

void
string
url(string $name, array $params = [])

Generates a path for a named route and appends unused parameters as a query string.

string
void
freeze()

Prevents further route and localized-route configuration changes.

void
bool

Reports whether route registration has become immutable.

bool
match(ServerRequestInterface $request, string|null $dispatchPath = null)

Matches the request method against its URI path or a dispatch-only path override.

array
allowedMethodsForPath(string $path)

Lists normalized HTTP methods that have a route for the supplied path.

array
bool
hasExplicitRouteForPath(HttpMethod|string $method, string $path)

Reports whether one method has an explicitly registered route for the path.

bool

Details

__construct

__construct()

Initializes an empty mutable route collection.

get

Route get(string $path, ControllerAction $action)

Registers a GET route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

post

Route post(string $path, ControllerAction $action)

Registers a POST route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

put

Route put(string $path, ControllerAction $action)

Registers a PUT route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

patch

Route patch(string $path, ControllerAction $action)

Registers a PATCH route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

delete

Route delete(string $path, ControllerAction $action)

Registers a DELETE route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

head

Route head(string $path, ControllerAction $action)

Registers a HEAD route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

options

Route options(string $path, ControllerAction $action)

Registers an OPTIONS route using the current group prefix.

Parameters

string $path
ControllerAction $action

Return Value

Route

getNamed

Route getNamed(string $name, string $path, ControllerAction $action)

Registers a named GET route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

postNamed

Route postNamed(string $name, string $path, ControllerAction $action)

Registers a named POST route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

putNamed

Route putNamed(string $name, string $path, ControllerAction $action)

Registers a named PUT route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

patchNamed

Route patchNamed(string $name, string $path, ControllerAction $action)

Registers a named PATCH route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

deleteNamed

Route deleteNamed(string $name, string $path, ControllerAction $action)

Registers a named DELETE route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

headNamed

Route headNamed(string $name, string $path, ControllerAction $action)

Registers a named HEAD route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

optionsNamed

Route optionsNamed(string $name, string $path, ControllerAction $action)

Registers a named OPTIONS route and reserves its URL-generation name.

Parameters

string $name
string $path
ControllerAction $action

Return Value

Route

map

Route map(HttpMethod|string $method, string $path, ControllerAction $action)

Adds one route definition and applies constraints configured for localized groups.

Parameters

HttpMethod|string $method
string $path
ControllerAction $action

Return Value

Route

mapNamed

Route mapNamed(string $name, HttpMethod|string $method, string $path, ControllerAction $action)

Adds one named route after rejecting a duplicate effective name.

Parameters

string $name
HttpMethod|string $method
string $path
ControllerAction $action

Return Value

Route

group

RouteGroup group(string $prefix, callable $builder)

Runs a registration callback with a temporary path prefix and returns its routes.

Parameters

string $prefix
callable $builder

Return Value

RouteGroup

localizedGroup

LocalizedRouteGroup localizedGroup(callable $builder)

Registers parallel plain and locale-prefixed variants for the callback's routes.

Parameters

callable $builder

Return Value

LocalizedRouteGroup

configureLocalizedRoutes

void configureLocalizedRoutes(string $routeNamePrefix = 'localized.', string|null $routePrefix = null, string $localeParameter = 'locale', array $supportedLocales = [])

Configures the static locale-prefix convention used by future localized groups.

It must run before route registration is frozen.

Parameters

string $routeNamePrefix
string|null $routePrefix
string $localeParameter
array $supportedLocales

Return Value

void

url

string url(string $name, array $params = [])

Generates a path for a named route and appends unused parameters as a query string.

Parameters

string $name
array $params

Return Value

string

freeze

void freeze()

Prevents further route and localized-route configuration changes.

Return Value

void

isFrozen

bool isFrozen()

Reports whether route registration has become immutable.

Return Value

bool

match

RouteMatch match(ServerRequestInterface $request, string|null $dispatchPath = null)

Matches the request method against its URI path or a dispatch-only path override.

The override changes only route selection; request metadata and the query-free path reported by a missing-route exception remain those of the original request.

Parameters

ServerRequestInterface $request
string|null $dispatchPath

Return Value

RouteMatch

allowedMethodsForPath

array allowedMethodsForPath(string $path)

Lists normalized HTTP methods that have a route for the supplied path.

Parameters

string $path

Return Value

array

hasExplicitRouteForPath

bool hasExplicitRouteForPath(HttpMethod|string $method, string $path)

Reports whether one method has an explicitly registered route for the path.

Parameters

HttpMethod|string $method
string $path

Return Value

bool