API / Minn\Http / Router

Router final class

Minn\Http\Router · public/minn/src/Minn/Http/Router.php · 260 lines

Matches a request to a #[Route] on one of the registered handler objects, has the check refuse a bad argument, has the gate judge the route's policy, and invokes the method with the request plus the named pattern captures. That order is the reference's: an invalid parameter is answered before the caller is looked at. The gate is the one thing a router cannot be built without: a policy nobody judges is a route nobody may call.

Constants and properties

privatearray $routes
privatereadonly Closure $gate

Promoted from the constructor.

privatereadonly ?Closure $check

Promoted from the constructor.

privatereadonly ?Minn\Http\Envelope $envelope

Promoted from the constructor.

Public

new__construct(Closure $gate, ?Closure $check = NULL, ?Minn\Http\Envelope $envelope = NULL)#
$envelopeEnvelope|nullwhat runs around a matched route instead of the router throwing its refusals itself
methodregister(object ...$handlers): self#

Registers every #[Route] method of the given handlers; returns the router for chaining.

methodroutes(): array#

The routes that list in an index: pattern => methods. @return array<string, list<string>>

@return array<string, list<string>>
methodtable(): array#

Every registered route as a row: what it is, who it is for, what it takes. The same rows Rest\Catalogue reads from the classes alone.

@return list<RouteRow>
methodallowed(Minn\Http\Request $request): array#

The methods the caller may use on this path, for the Allow header: every route matching the path (a route with no captures that names the path exactly claims it alone), its policy judged for the caller without dispatching. A route that states no policy counts for GET only, until it states one. Empty when nothing matched or nothing is allowed, and the header is then left out, as the reference does.

@return list<string> in the reference's order
methodmatching(Minn\Http\Request $request): array#

The routes a request's method and path match, policies unjudged, in the order dispatch tries them, with their captures: what a batch reads to tell a route that does not take part from no route at all.

@return list<array{0: Route, 1: array<string, string>}>
methoddispatch(Minn\Http\Request $request): ?Minn\Http\Response#

Null when nothing matched, so the caller can fall through.

Internals 7

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privateadmits(Minn\Http\Policy $policy, Minn\Http\Request $request, array $captures): bool#
$capturesarray<string, string>
privatearguments(ReflectionMethod $method, array $captures): array#

The captures a handler takes, by parameter name; a capture spelled as the reference spells it (user_id) binds to its camelCase parameter.

$capturesarray<string, string>
@return array<string, string>
privateanswer(Minn\Http\Route $route, Minn\Http\Request $request, array $captures, Closure $invoke): Minn\Http\Response#
$capturesarray<string, string>
$invokeClosure(): Response
privateenveloped(Minn\Http\Matched $matched, Minn\Http\Request $request, Closure $invoke): Minn\Http\Response#

The refusals judged up front and handed over unthrown. The policy is judged even past a bad argument, because the envelope may clear that; a policy that declines the route throws before anything hears of it.

$invokeClosure(): Response
privatemethodsOf(object $handler, string $name, string $pattern, Minn\Http\Method $asked): array#

The methods one handler answers on one pattern alongside the request's, grouped as the reference registers them: reading alone, deleting alone, and creating or editing together (POST, PUT, PATCH).

@return list<string>
privateliteral(Minn\Http\Route $route): ?string#

The one path a route spells, a capture it fixes ({base:media}) read as its word; null for a route with a real capture.

privatespells(Minn\Http\Route $route, string $path): bool#

Whether a route names a path in so many words: its captures, if any, only choose among words ({base:categories|tags}), so the path is one of the routes it spells rather than a value it captures.