Repository navigation
Internals 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.
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;class Router implements \router\interface\IRouteSource 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.
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 | 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 |
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(),
)| 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);
}public function setMethod(string $method): self| 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;
}public function getMethod(): stringReturn expressions (including local callbacks): $this->method.
Implementation, validation guards and interaction order
public function getMethod(): string
{
return $this->method;
}public function withoutCsrf(): selfState writes: $this->csrf.
Return expressions (including local callbacks): $this.
Implementation, validation guards and interaction order
public function withoutCsrf(): self
{
$this->csrf = false;
return $this;
}public function requiresCsrf(): boolReturn expressions (including local callbacks): $this->csrf.
Implementation, validation guards and interaction order
public function requiresCsrf(): bool
{
return $this->csrf;
}public function setName(string $name): self| 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;
}public function setAlias(string $alias): self| 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;
}public function addEventDispatcher(EventDispatcher $eventDispatcher): self| 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;
}public function addMiddleware(IMiddleware $middleware): self| 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;
}public function getMiddleware(): ?IMiddlewareReturn expressions (including local callbacks): $this->middleware.
Implementation, validation guards and interaction order
public function getMiddleware(): ?IMiddleware
{
return $this->middleware;
}public function getName(): ?stringReturn expressions (including local callbacks): $this->name.
Implementation, validation guards and interaction order
public function getName(): ?string { return $this->name; }public function getPath(): stringReturn expressions (including local callbacks): $this->path.
Implementation, validation guards and interaction order
public function getPath(): string { return $this->path; }public function getAction(): stringReturn expressions (including local callbacks): $this->action.
Implementation, validation guards and interaction order
public function getAction(): string { return $this->action; }public function getAlias(): ?stringReturn expressions (including local callbacks): $this->alias.
Implementation, validation guards and interaction order
public function getAlias(): ?string { return $this->alias; }public function isService(): boolReturn expressions (including local callbacks): $this->service.
Implementation, validation guards and interaction order
public function isService(): bool { return $this->service; }public function getController(): ObjectFactory|ControllerReturn expressions (including local callbacks): $this->controller.
Implementation, validation guards and interaction order
public function getController(): ObjectFactory|Controller { return $this->controller; }public function getEventDispatcher(): EventDispatcherReturn expressions (including local callbacks): $this->eventDispatcher.
Implementation, validation guards and interaction order
public function getEventDispatcher(): EventDispatcher { return $this->eventDispatcher; }public function setController(ObjectFactory|Controller $controller): self| 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;
}public function match(string $requestPath): ?array| 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);
}public function route(array $params = [], ?IRequest $request = null): View|Redirect|Response| 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);
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes