Router
Router
Central HTTP router for application request dispatch.
class Router implements \Hazaar\Application\Interface\RouterImplements: Hazaar\Application\Interface\Router
The router coordinates route registration, route loading, request matching, middleware execution, and final controller dispatch for the current application instance.
Core responsibilities:
- Load route definitions through a pluggable loader (
typein router config). - Provide static helpers for HTTP verb route registration (
get,post,match, etc.). - Resolve requests in a deterministic order:
- Internal controller shortcuts (
self::$internal). - Loader-provided route match (
$routeLoader->evaluateRequest()). - In-memory routes added at runtime (
$this->routes). - Default controller/action fallback for the root path (
/).
- Internal controller shortcuts (
- Build a middleware pipeline and execute the resolved controller action.
- Emit structured request lifecycle logs for route match, dispatch, and completion.
Instance access:
- The class supports static access through
getInstance()and can also hold an explicit static instance (setInstance/clearInstance) for loader and helper integrations.
Properties
internal
Internal controllers.
public array $internal = array (
'hazaar' => '\\Hazaar\\Controller\\Internal',
)Type: array
config
public Options $configType: Hazaar\Application\Router\Options
routeLoader
Active route loader implementation used for bootstrap-time and optional
private Loader $routeLoaderType: Hazaar\Application\Router\Loader
request-time route resolution.
middlewareDispatcher
Middleware dispatcher responsible for executing per-route middleware stacks
private Dispatcher $middlewareDispatcherType: Hazaar\Middleware\Dispatcher
around the final controller handler.
routes
private array $routesType: array
initialised
Guards Hazaar\Application\Router::initialise against running the route loader more than once —
private bool $initialisedType: bool
important now that Hazaar\Application\Router::getRoutes may trigger it lazily outside the normal bootstrap flow (for example from the OpenAPI generator), where re-running a file-type loader would re-include route.php and duplicate every route.
matchers
Registered custom parameter matchers, keyed by name.
private array $matchersType: array
Each matcher is either a PCRE pattern string or a \Closure that accepts the raw path segment and returns true when it matches. Matchers only validate a segment's format; they never transform the value.
builtinMatchers
Built-in matchers registered on every router instance.
private array $builtinMatchers = array (
'uuid' => '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i',
'ulid' => '/^[0-7][0-9A-HJKMNP-TV-Z]{25}$/i',
'slug' => '/^[a-z0-9]+(?:-[a-z0-9]+)*$/',
'date' => '/^\\d{4}-\\d{2}-\\d{2}$/',
)Type: array
These are seeded in the constructor and can be overridden by calling Hazaar\Application\Router::addMatcher with the same name.
instance
public self $instanceType: self
Methods
__construct
public __construct(Options $config = 'new Options()', array $middlewareAliases): voidParameters
| Parameter | Type | Description |
|---|---|---|
$config | Hazaar\Application\Router\Options | |
$middlewareAliases | array |
getInstance
Get the current router instance.
public getInstance(): ?selfReturns: self
This method retrieves the singleton instance of the Router from the main Application.
setInstance
Sets the global instance of the Router.
public setInstance(self $router): voidReturns: void
This method allows setting the singleton instance of the Router class. It is typically used by router loaders that require a global router instance accessible through the Router class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$router | self | the router instance to set as the global instance |
clearInstance
Clears the current Singleton instance.
public clearInstance(): voidReturns: void
This method resets the static instance variable to null, effectively clearing the current instance of the class. It is typically used by router loaders that require a global router instance accessible through the Router class.
initialise
public initialise(): boolReturns: bool
handle
Handles an incoming HTTP request by evaluating the route, applying middleware, and executing the controller logic.
public handle(Request $request): Response|RouteReturns: Hazaar\Controller\Response|Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request | the incoming HTTP request to handle |
addRoute
Adds a route to the router.
public addRoute(Route $route): voidReturns: void
This method sets the router for the given route and then adds the route to the list of routes managed by this router.
Parameters
| Parameter | Type | Description |
|---|---|---|
$route | Hazaar\Application\Route | the route to be added |
add
Adds a route to the router.
public add(Route $route): voidReturns: void
This method is a static convenience wrapper that adds a new route instance to the singleton instance of the Router.
Parameters
| Parameter | Type | Description |
|---|---|---|
$route | Hazaar\Application\Route | the route object to add |
getRoutes
Returns every route this application can currently dispatch, regardless of which router
public getRoutes(): arrayReturns: array
type (file, basic, advanced, attribute, json) produced it.
This is a static, request-independent enumeration — unlike Hazaar\Application\Router::evaluateRequest, which resolves a single route for one incoming path. Router types that declare routes explicitly (file, json, attribute) return their full declared set; basic/advanced, which have no static declaration surface at all, return best-effort routes synthesized from scanning the app's controllers directory (see Hazaar\Application\Router\Loader::getRoutes and its per-loader overrides).
Ensures the loader has had a chance to run first (Hazaar\Application\Router::initialise, idempotent).
registeredRoutes
Returns the routes currently registered directly on this router (via Hazaar\Application\Router::addRoute
public registeredRoutes(): arrayReturns: array
/ Hazaar\Application\Router::add), without involving the route loader at all.
This is the storage a Router\Loader's default Hazaar\Application\Router\Loader::getRoutes implementation reads from — kept as a distinctly-named method (rather than reusing Hazaar\Application\Router::getRoutes) so the loader's default doesn't recurse back into itself.
addMatcher
Registers a custom parameter matcher.
public addMatcher(string $name, \Closure|string $matcher): voidReturns: void
A matcher constrains a route parameter to a specific format rather than a PHP type, allowing routes such as /get/<uuid:id> and /get/history to coexist unambiguously. Reference a registered matcher in a route the same way a type is used: <matcherName:paramName>.
Matchers are validate-only — they decide whether a segment matches but never transform its value. The raw string is passed to the controller; use a native type (for example <int:id>) when coercion is required.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the matcher name, as referenced in route patterns |
$matcher | Closure | string |
matcher
Static convenience wrapper for Hazaar\Application\Router::addMatcher.
public matcher(string $name, \Closure|string $matcher): voidReturns: void
Intended for use in route definition files where routes are registered through the static helpers (Router::get(), Router::match(), ...).
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the matcher name, as referenced in route patterns |
$matcher | Closure | string |
hasMatcher
Determines whether a matcher with the given name is registered.
public hasMatcher(string $name): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the matcher name |
matchValue
Tests a value against a registered matcher.
public matchValue(string $name, string $value): boolReturns: bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | the matcher name |
$value | string | the raw path segment to validate |
getErrorController
Retrieves the error controller instance.
public getErrorController(?Request $originalRequest): ErrorReturns: Hazaar\Controller\Error
This method checks the configuration for a specified error controller class. If the class exists and is a subclass of the Error class, it instantiates and returns the error controller. If no valid error controller is found in the configuration, it returns a default Error instance.
A fresh Hazaar\Application\Request is always used for the error controller itself (so it isn't dispatched with, e.g., stale session state), but when the request that triggered the error already matched a route, that route's declared response type (its return type hint, see Hazaar\Application\Route::getResponseType) is carried over onto the fresh request. This keeps error output consistent with what the target controller would have returned, even when the client's Accept header is missing or ambiguous, or the failure happened before the target controller itself had a chance to run.
Parameters
| Parameter | Type | Description |
|---|---|---|
$originalRequest | Hazaar\Application\Request | the request that was being processed when the error occurred, if available |
get
Registers a route that responds to HTTP GET requests.
public get(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URL path for the route |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
post
Registers a route that responds to HTTP POST requests.
public post(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URL path for the route |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
put
Registers a route that responds to HTTP PUT requests.
public put(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URI path that the route will respond to |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
delete
Registers a route that responds to HTTP DELETE requests.
public delete(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URL path that the route should match |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
patch
Registers a route that responds to HTTP PATCH requests.
public patch(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URI path that the route will match |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
options
Registers a route that responds to HTTP OPTIONS requests.
public options(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the URL path to match |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
any
Registers a route that responds to any HTTP method.
public any(string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | the path pattern to match |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
group
Groups routes under a common URL prefix.
public group(string $prefix, \Closure $callback): GroupReturns: Hazaar\Application\Router\Group
This method allows you to group multiple route definitions that share a common URL path prefix. The provided callback function will be executed with the router instance, allowing routes defined within to inherit the prefix logic (though currently the prefix appears unused in this implementation).
Parameters
| Parameter | Type | Description |
|---|---|---|
$prefix | string | the URL prefix for the grouped routes |
$callback | Closure | a callback function that receives the Router instance |
Matches a route with the given HTTP methods, path, and callable.
public (array|string|null $methods, string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$methods | array | string |
$path | string | The path to match (e.g., '/user/{id}'). |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
matchRoute
Matches a route with the given HTTP methods, path, and callable.
public matchRoute(array|string|null $methods, string $path, \Closure|\ReflectionMethod|array|string $callable, array $actionArgs): RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$methods | array | string |
$path | string | The path to match (e.g., '/user/{id}'). |
$callable | Closure | ReflectionMethod |
$actionArgs | array | optional action arguments for the route |
evaluateRequest
Evaluates the given request and matches it against the defined routes.
public evaluateRequest(Request $request): ?RouteReturns: Hazaar\Application\Route
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Hazaar\Application\Request | the request to evaluate |
Generated by Hazaar API Doc Generator on Fri, 04 Sep 2026 23:32:17 +0000