Skip to content

Core\Router::dispatch

Calls the method of the route that the request matched. You call Core\Router::dispatch once, in the file that answers requests. You give it your gate: an instance of a class that implements Core\Router\Gate.

The gate has three methods. allows runs before each route method, and the method runs only when it returns true. refused writes the page when no route method runs, such as a missing page. failed writes the page when the route method throws an error. dispatch sets the status first, so the right code is sent.

A capture is a value from the path, such as 42 in /articles/42. Each capture and #[Core\Query] value is converted to its parameter's type before allows runs. allows reads them with $route->parameter().

Good to know: if allows throws an error, the route method does not run. When a route method returns a value, a gate that implements Core\Router\Responder sends it with respond. A plain gate does not use the value, and nvs check gives a warning.

The examples below show two routes, a gate that checks a login, and an error page.

Parameters

  • $gateCore\Router\GateAn instance of your class that implements Core\Router\Gate.

Return value

Returns void. Nothing. The route method or the gate writes the response, for example with echo or Core\Response::sendHtml. When the gate implements Core\Router\Responder, dispatch passes the value that a route method returns to $gate->respond(). Otherwise dispatch does not send that value. An error that refused(), failed() or respond() throws is thrown by dispatch.

Errors

  • LogicErrorWhen the program has no #[Route]. When the program is not handling a request, for example a command-line program, a job or a test. When no capture and no query value fills a parameter of the route method. An optional capture that the path does not have is an example.

Changelog

VersionChange
0.0.6A route parameter of type Core\Queue\Id now gets the id from the link. Before, every request to such a route gave the status 500.

Examples

Call the method of a route

<?nvs
// Shows how `Core\Router::dispatch` calls the method of the route that a request matched.
use Core\Http\Method;
use Core\Router\Refusal;
class Books
{
#[Core\Route(path: '/books', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function list(): void
{
echo 'all books';
}
// The value of `{id}` in the path goes to the parameter `$id`.
#[Core\Route(path: '/books/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $id): void
{
echo 'book ', $id;
}
}
// The gate checks each request before a route method runs, and it writes the error pages.
class Gate implements Core\Router\Gate
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Refusal $reason, ?Core\Router\Match $route): void
{
echo 'no page here';
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'something went wrong';
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// From the command line, this part sends three requests. It needs no server.
// This prints `200 all books`, then `200 book 7`, then `404 no page here`.
var $list = Core\Test::request(Method::Get, '/books');
echo $list->status(), ' ', $list->body(), "\n";
var $one = Core\Test::request(Method::Get, '/books/7');
echo $one->status(), ' ', $one->body(), "\n";
var $missing = Core\Test::request(Method::Get, '/films');
echo $missing->status(), ' ', $missing->body(), "\n";
}
Output
200 all books
200 book 7
404 no page here

A gate that checks a login

<?nvs
// Shows a gate that lets only a logged-in user open the admin pages.
use Core\Http\Method;
use Core\Router\Refusal;
enum Role
{
Visitor = 1,
Admin = 2,
}
class Pages
{
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Role::Visitor)]
public function home(): void
{
echo 'home page';
}
#[Core\Route(path: '/admin', method: Core\Http\Method::Get)]
#[Core\Access(allow: Role::Admin)]
public function admin(): void
{
echo 'admin page';
}
}
class Gate implements Core\Router\Gate
{
// `accessAs<Role>()` returns the `allow:` value of the route.
// An admin route runs only when the request has the right token.
public function allows(Core\Router\Match $route): bool
{
if ($route->accessAs<Role>() == Role::Visitor) {
return true;
}
return Core\Request::header('x-admin-token') == 'letmein';
}
// When `allows` returns `false`, the reason is `Refusal::Denied` and the status is 403.
public function refused(Refusal $reason, ?Core\Router\Match $route): void
{
if ($reason == Refusal::Denied) {
echo 'please log in';
} else {
echo 'no page here';
}
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'something went wrong';
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// This prints `200 home page`, then `403 please log in`, then `200 admin page`.
var $home = Core\Test::request(Method::Get, '/');
echo $home->status(), ' ', $home->body(), "\n";
var $denied = Core\Test::request(Method::Get, '/admin');
echo $denied->status(), ' ', $denied->body(), "\n";
var $admin = Core\Test::request(Method::Get, '/admin', {
headers: ['X-Admin-Token' => 'letmein'],
});
echo $admin->status(), ' ', $admin->body(), "\n";
}
Output
200 home page
403 please log in
200 admin page

An error page when a method fails

<?nvs
// Shows how the gate writes one error page when a route method throws an error.
use Core\Http\Method;
use Core\Router\Refusal;
class Reports
{
#[Core\Route(path: '/reports/{year}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $year): void
{
if ($year < 2000) {
throw new RuntimeError('no report before 2000');
}
echo 'report for ', $year;
}
}
class Gate implements Core\Router\Gate
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Refusal $reason, ?Core\Router\Match $route): void
{
echo 'no page here';
}
// `dispatch` has set the status to 500. `$error` is the error the method threw.
// `is` checks which method the route belongs to, so the page can say which page failed.
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
if ($route != null && $route->is(Reports::show)) {
echo 'the report page failed';
} else {
echo 'a page failed';
}
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Gate());
} else {
// This prints `200 report for 2024`, then `500 the report page failed`.
var $good = Core\Test::request(Method::Get, '/reports/2024');
echo $good->status(), ' ', $good->body(), "\n";
var $bad = Core\Test::request(Method::Get, '/reports/1999');
echo $bad->status(), ' ', $bad->body(), "\n";
}
Output
200 report for 2024
500 the report page failed