Core\Router
The routes of your program. url(), urlAbsolute() and urlSigned() build a link to the route of a method. match() and methodsFor() check a path against the routes, and signedRoute() checks the signature of a signed link. dispatch() calls the method of the route that the request matched.
Core\Router::url builds a link to the route of a method, written Users::show with no (...): each {capture} in the path is substituted from $params, percent-encoded into its own segment, and $params may be left out for a route with no captures. It is only meaningful in a program that declares routes — the attributes chapter owns the declaration, and every #[Core\Route] needs a sibling #[Core\Access]. A method written directly in the code is checked while compiling, and a method with no route is a compile error. A method from a variable, such as an array<callable> or $users->show(...), is checked while the program runs, and the call throws an error when it has no route. A $params array missing a capture or carrying a key that is neither a capture nor a handler parameter declared #[Core\Query] is a compile error. A key that is declared #[Core\Query] becomes the link's query string, /users/7?page=2. urlAbsolute is url with the [[app]] origin from nvs.toml in front, and throws RuntimeError when none is configured.
A link may name a route of another module, which is another program on the same server. The linking program declares that module's classes with autoload links, and may import such a class with use. The compiler checks the method and its captures the same way, and the server picks the mount the link goes through. urlSigned cannot name such a route, and that call does not compile. The server chapter's section Links between modules has the whole picture.
methodsFor asks the table the other question, the one Core\Request::route() leaves open when it answers null: which verbs does this path claim? An empty array means no route claims it at all — the 404 — and a non-empty one is the list an Allow: header spells alongside a 405. Both forms of a terminal {page?} answer the same verbs, so /posts and /posts/3 are never a 404 and a 405 for one route. Nothing about it is a request: it is asked of the table, about a path, and the program decides what to send.
[[app]]root = "."origin = "https://example.test"<?nvsclass Users{ #[Core\Route(path: '/users', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function index(): string { return 'every user'; }
#[Core\Route(path: '/users/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id): string { return 'user ' . $id; }
#[Core\Route(path: '/users/{id}/files/{path...}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function file(uint $id, string $path): string { return $path; }}
echo Core\Router::url(Users::index), "\n";echo Core\Router::url(Users::show, ['id' => 7]), "\n";echo Core\Router::url(Users::file, ['id' => 7, 'path' => 'a b/c.txt']), "\n";echo Core\Router::urlAbsolute(Users::show, ['id' => 7]), "\n";/users/users/7/users/7/files/a%20b/c.txthttps://example.test/users/7Members
7 members.
| Member | Signature |
|---|---|
url | url(callable $route, array<mixed> $params = []): string |
urlAbsolute | urlAbsolute(callable $route, array<mixed> $params = []): string |
urlSigned | urlSigned(callable $route, array<mixed> $params, { keys: array<secret bytes>, until: ?Core\Time\Instant } $settings): string |
signedRoute | signedRoute(array<secret bytes> $keys): Core\Router\Match |
match | match(Core\Http\Method $method, string $path): ?Core\Router\Match |
methodsFor | methodsFor(string $path): array<Core\Http\Method> |
dispatch | dispatch(Core\Router\Gate $gate): void |
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.
