Skip to content

Core\Router\Match

The route that a path matched. Core\Request::route() returns the match of the request being served, and Core\Router::match() returns the match of a path you choose. is() checks which method the route belongs to. parameters() and parameter() return the values captured from the path, and the query values inside Core\Router::dispatch. method() returns the route's HTTP method. accessAs<E>() returns the route's access rule as a case of the enum E, and access() returns the same rule as its name. attribute<T>() returns an attribute of the route's method.

A Core\Router\Match is what Core\Request::route() answers (null when nothing in the route table claimed this method and path); it is never constructed by hand. The server matches the incoming request against the compiled table once, before any of the program runs, and this is that result travelling on the request — so a handler never matches its own path a second time. Matching is not dispatching: nothing here calls the annotated method, and a match has nothing that can be called. The program calls the method itself, after it checks which method matched with is(); the attributes chapter shows that check, and how the program sends the 404 and the 405.

is(Shop::show) returns true when the route belongs to that method. You write the method the way Core\Router::url takes it: Class::method with no (...), for a static or an instance method. A method from a variable, such as an array<callable> or $shop->show(...), is checked while the program runs, and is throws an error when that method has no route. parameters() returns every value the path filled, keyed by the name of its parameter. parameter() returns one of them by name. It returns null for a name the route does not declare, and for an optional {name?} the request did not include. Inside Core\Router::dispatch, the match also has each #[Core\Query] value, under its parameter's name. So Gate::allows can check every value the route method gets. The match from Core\Request::route() has no query values, so parameter() returns null for a query name there. A capture is tainted string where the route declared string, still percent-encoded, and the int, uint, decimal or Core\Uuid the match already converted where it declared one of those, so a handler never parses a segment the router has parsed already — and a segment that would not convert did not match the route in the first place.

access() is the route's #[Core\Access(allow: …)] as a string: the full name of the constant, such as Core\Audience::Public or App\Role::Admin. The server does not enforce it. A program that calls route methods checks it once, before the switch, so the check also covers a route added later. Treat null as access denied. method() is the verb the matched #[Core\Route] declares, which tells apart the routes of one method that serves several verbs.

Reading it needs a request. Off the command line — and in a scheduled script, a job worker or a test — there is none, and the reader refuses rather than answering null, because "no request arrived" and "nothing matched" are different facts.

<?nvs
try {
Core\Request::route();
} catch (LogicError $why) {
echo "no request here\n";
}
Output
no request here

Returned by

You get a Core\Router\Match from these methods:

Members

7 members.

MemberSignature
isis(callable $route): bool
parametersparameters(): array<tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|Parses>
parameterparameter(string $name): ?tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|Parses
methodmethod(): Core\Http\Method
accessaccess(): ?string
accessAsaccessAs<T>(): ?T
attributeattribute<T>(): ?T

Example apps

These complete apps use this class. You can download and run each one.

  • Chat: A chat app with rooms, WebSockets and SQLite. One Novis server sends the pages and runs every connection, with no separate WebSocket server and no library.
  • Notes: A small notes app with SQLite, HTML pages, a JSON API, form checks and tests. Download it, run it and start your own app from it.
  • Poll: A live poll with SQLite and server-sent events. Every open page shows each new vote at once, with no extra server and no library.