Skip to content

Internals Router Router

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

router\Router

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/router/Router.php · Snapshot 8411f4287706.

Routing separates definition, HTTP request snapshot, matching, method policy, execution and error responses. Collaborators can be replaced through constructor injection.

Imports and dependencies

use controller\Controller;
use event\EventDispatcher;
use middleware\IMiddleware;
use object\ObjectFactory;
use redirect\Redirect;
use response\Response;
use view\View;
use router\execution\RouteExecutor;
use router\http\HttpMethod;
use router\interface\IRequest;
use router\interface\IRequestProvider;
use router\interface\IMethodPolicy;
use router\http\GlobalsRequestProvider;
use router\interface\IPathMatcher;
use router\interface\IRouteExecutor;
use router\matching\SegmentPathMatcher;

Router

Declaration and architectural role

class Router implements \router\interface\IRoute

Source description (behavioral corrections are recorded in Known discrepancies):

Route definition and fluent configuration. Matching and execution are delegated to replaceable collaborators; the existing route API is preserved.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$name public ?string $name; @var ?string $name The name of the route, if defined.
$path public string $path; @var string $path The path pattern for the route.
$action public string $action; @var string $action The action name to be executed when the route is matched.
$alias public ?string $alias; @var ?string $alias An optional alias for the route.
$controller public ObjectFactory|Controller $controller; @var ObjectFactory|Controller $controller The controller instance associated with the route.
$eventDispatcher public EventDispatcher $eventDispatcher; @var EventDispatcher $eventDispatcher The event dispatcher to handle events for the route.
$middleware private ?IMiddleware $middleware; Used by __construct(), addMiddleware(), getMiddleware().
$shared public array $shared = []; Declared extension/state field; no own method directly consumes it in this source file.
$service public bool $service = false; @var bool Service Determines if this route is used for processing data or other type of jobs, and should be hidden from UI.
$method private string $method = 'GET'; Used by setMethod(), getMethod().
$csrf private bool $csrf = true; Used by withoutCsrf(), requiresCsrf().
$pathMatcher private readonly IPathMatcher $pathMatcher; Used by __construct(), match().
$executor private readonly IRouteExecutor $executor; Used by __construct(), route().
Constructor-promoted private readonly IRequestProvider $requests = new GlobalsRequestProvider() Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly IMethodPolicy $methods = new HttpMethod() Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( string $path, string $action, ObjectFactory|Controller $controller, bool $service = false, ?IPathMatcher $pathMatcher = null, ?IRouteExecutor $executor = null, private readonly IRequestProvider $requests = new GlobalsRequestProvider(), private readonly IMethodPolicy $methods = new HttpMethod(), )
setMethod public function setMethod(string $method): self
getMethod public function getMethod(): string
withoutCsrf public function withoutCsrf(): self
requiresCsrf public function requiresCsrf(): bool
setName public function setName(string $name): self
setAlias public function setAlias(string $alias): self
addEventDispatcher public function addEventDispatcher(EventDispatcher $eventDispatcher): self
addMiddleware public function addMiddleware(IMiddleware $middleware): self
getMiddleware public function getMiddleware(): ?IMiddleware
getName public function getName(): ?string
getPath public function getPath(): string
getAction public function getAction(): string
getAlias public function getAlias(): ?string
isService public function isService(): bool
getController public function getController(): ObjectFactory|Controller
getEventDispatcher public function getEventDispatcher(): EventDispatcher
setController public function setController(ObjectFactory|Controller $controller): self
match public function match(string $requestPath): ?array
route public function route(array $params = [], ?IRequest $request = null): View|Redirect|Response

__construct

Source line 86

public function __construct(
        string $path,
        string $action,
        ObjectFactory|Controller $controller,
        bool $service = false,
        ?IPathMatcher $pathMatcher = null,
        ?IRouteExecutor $executor = null,
        private readonly IRequestProvider $requests = new GlobalsRequestProvider(),
        private readonly IMethodPolicy $methods = new HttpMethod(),
    )

Parameters

Parameter Type, modifiers and default Purpose
$path string $path The path pattern for the route.
$action string $action The action to be called.
$controller ObjectFactory|Controller $controller The controller handling the action.
$service bool $service = false */
$pathMatcher ?IPathMatcher $pathMatcher = null Operation input; the exact use, validation and propagation are shown in the implementation trace.
$executor ?IRouteExecutor $executor = null Operation input; the exact use, validation and propagation are shown in the implementation trace.
$requests private readonly IRequestProvider $requests = new GlobalsRequestProvider() Operation input; the exact use, validation and propagation are shown in the implementation trace.
$methods private readonly IMethodPolicy $methods = new HttpMethod() Operation input; the exact use, validation and propagation are shown in the implementation trace.

Router constructor.

@param string $path The path pattern for the route. @param string $action The action to be called. @param ObjectFactory|Controller $controller The controller handling the action. @param bool $service

State writes: $this->path, $this->action, $this->controller, $this->name, $this->alias, $this->middleware, $this->service, $this->pathMatcher, $this->executor.

Implementation, validation guards and interaction order
/**
     * Router constructor.
     *
     * @param string $path The path pattern for the route.
     * @param string $action The action to be called.
     * @param ObjectFactory|Controller $controller The controller handling the action.
     * @param bool $service
     */
    public function __construct(
        string $path,
        string $action,
        ObjectFactory|Controller $controller,
        bool $service = false,
        ?IPathMatcher $pathMatcher = null,
        ?IRouteExecutor $executor = null,
        private readonly IRequestProvider $requests = new GlobalsRequestProvider(),
        private readonly IMethodPolicy $methods = new HttpMethod(),
    ) {
        $this->path = $path;
        $this->action = $action;
        $this->controller = $controller;
        $this->name = "";
        $this->alias = "";
        $this->middleware = null;
        $this->service = $service;

        $this->pathMatcher = $pathMatcher ?? new SegmentPathMatcher();
        $this->executor = $executor ?? new RouteExecutor(methods: $this->methods);
    }

setMethod

Source line 109

public function setMethod(string $method): self

Parameters

Parameter Type, modifiers and default Purpose
$method string $method HTTP method token; validation/normalization follows the method policy.

State writes: $this->method.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setMethod(string $method): self
    {
        $this->method = $this->methods->normalize($method);
        return $this;
    }

getMethod

Source line 115

public function getMethod(): string

Return expressions (including local callbacks): $this->method.

Implementation, validation guards and interaction order
public function getMethod(): string
    {
        return $this->method;
    }

withoutCsrf

Source line 120

public function withoutCsrf(): self

State writes: $this->csrf.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function withoutCsrf(): self
    {
        $this->csrf = false;
        return $this;
    }

requiresCsrf

Source line 126

public function requiresCsrf(): bool

Return expressions (including local callbacks): $this->csrf.

Implementation, validation guards and interaction order
public function requiresCsrf(): bool
    {
        return $this->csrf;
    }

setName

Source line 138

public function setName(string $name): self

Parameters

Parameter Type, modifiers and default Purpose
$name string $name The name to be set.

Sets the name of the route.

@param string $name The name to be set. @return self The Router instance for method chaining.

State writes: $this->name.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets the name of the route.
     *
     * @param string $name The name to be set.
     * @return self The Router instance for method chaining.
     */
    public function setName(string $name): self
    {
        $this->name = $name;
        return $this;
    }

setAlias

Source line 150

public function setAlias(string $alias): self

Parameters

Parameter Type, modifiers and default Purpose
$alias string $alias The alias to be set.

Sets an alias for the route.

@param string $alias The alias to be set. @return self The Router instance for method chaining.

State writes: $this->alias.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Sets an alias for the route.
     *
     * @param string $alias The alias to be set.
     * @return self The Router instance for method chaining.
     */
    public function setAlias(string $alias): self
    {
        $this->alias = $alias;
        return $this;
    }

addEventDispatcher

Source line 162

public function addEventDispatcher(EventDispatcher $eventDispatcher): self

Parameters

Parameter Type, modifiers and default Purpose
$eventDispatcher EventDispatcher $eventDispatcher The event dispatcher to be set.

Adds an event dispatcher to the router.

@param EventDispatcher $eventDispatcher The event dispatcher to be set. @return self The Router instance for method chaining.

State writes: $this->eventDispatcher.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Adds an event dispatcher to the router.
     *
     * @param EventDispatcher $eventDispatcher The event dispatcher to be set.
     * @return self The Router instance for method chaining.
     */
    public function addEventDispatcher(EventDispatcher $eventDispatcher): self
    {
        $this->eventDispatcher = $eventDispatcher;
        return $this;
    }

addMiddleware

Source line 169

public function addMiddleware(IMiddleware $middleware): self

Parameters

Parameter Type, modifiers and default Purpose
$middleware IMiddleware $middleware Middleware object or class name according to the component; route and storage return handling differ.

State writes: $this->middleware.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function addMiddleware(IMiddleware $middleware): self
    {
        $this->middleware = $middleware;
        return $this;
    }

getMiddleware

Source line 175

public function getMiddleware(): ?IMiddleware

Return expressions (including local callbacks): $this->middleware.

Implementation, validation guards and interaction order
public function getMiddleware(): ?IMiddleware
    {
        return $this->middleware;
    }

getName

Source line 180

public function getName(): ?string

Return expressions (including local callbacks): $this->name.

Implementation, validation guards and interaction order
public function getName(): ?string { return $this->name; }

getPath

Source line 181

public function getPath(): string

Return expressions (including local callbacks): $this->path.

Implementation, validation guards and interaction order
public function getPath(): string { return $this->path; }

getAction

Source line 182

public function getAction(): string

Return expressions (including local callbacks): $this->action.

Implementation, validation guards and interaction order
public function getAction(): string { return $this->action; }

getAlias

Source line 183

public function getAlias(): ?string

Return expressions (including local callbacks): $this->alias.

Implementation, validation guards and interaction order
public function getAlias(): ?string { return $this->alias; }

isService

Source line 184

public function isService(): bool

Return expressions (including local callbacks): $this->service.

Implementation, validation guards and interaction order
public function isService(): bool { return $this->service; }

getController

Source line 185

public function getController(): ObjectFactory|Controller

Return expressions (including local callbacks): $this->controller.

Implementation, validation guards and interaction order
public function getController(): ObjectFactory|Controller { return $this->controller; }

getEventDispatcher

Source line 186

public function getEventDispatcher(): EventDispatcher

Return expressions (including local callbacks): $this->eventDispatcher.

Implementation, validation guards and interaction order
public function getEventDispatcher(): EventDispatcher { return $this->eventDispatcher; }

setController

Source line 188

public function setController(ObjectFactory|Controller $controller): self

Parameters

Parameter Type, modifiers and default Purpose
$controller ObjectFactory|Controller $controller Controller instance or lazy ObjectFactory depending on the union type.

State writes: $this->controller.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setController(ObjectFactory|Controller $controller): self
    {
        $this->controller = $controller;
        return $this;
    }

match

Source line 195

public function match(string $requestPath): ?array

Parameters

Parameter Type, modifiers and default Purpose
$requestPath string $requestPath Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return captured path parameters, or null when the path does not match.

Return expressions (including local callbacks): $this->pathMatcher->match($this->path, $requestPath).

Implementation, validation guards and interaction order
/** Return captured path parameters, or null when the path does not match. */
    public function match(string $requestPath): ?array
    {
        return $this->pathMatcher->match($this->path, $requestPath);
    }

route

Source line 201

public function route(array $params = [], ?IRequest $request = null): View|Redirect|Response

Parameters

Parameter Type, modifiers and default Purpose
$params array $params = [] Operation input; the exact use, validation and propagation are shown in the implementation trace.
$request ?IRequest $request = null Request snapshot consumed by matching, method and security checks.

Execute this route using the supplied request or the current HTTP request.

Return expressions (including local callbacks): $this->executor->execute($this, $request ?? $this->requests->current(), $params).

Implementation, validation guards and interaction order
/** Execute this route using the supplied request or the current HTTP request. */
    public function route(array $params = [], ?IRequest $request = null): View|Redirect|Response
    {
        return $this->executor->execute($this, $request ?? $this->requests->current(), $params);
    }

Related types

Clone this wiki locally