Skip to content

Core\Request::route

Returns the route that the request matched. A route is a method of your program with a #[Core\Route] attribute, such as #[Core\Route(path: "/users/{id}", method: Core\Http\Method::Get)]. The server compares the request with every route once, before your program starts. route() returns the result of that comparison, so your program does not compare the path again.

The result is a Core\Router\Match. Its method is(Users::show) checks whether the route belongs to the method Users::show. parameter("id") returns the value of the part {id} in the path. That value already has the type that the handler declares, so /users/42 gives the number 42 for a uint $id.

The result is null when no route matches the method and the path, and also when the program declares no routes. A command-line program answers no request, so route() throws a LogicError there. When a captured part decodes to bytes that are not valid UTF-8 text, route() throws a RuntimeError.

The examples below read a matched route, show an address that no route matches, and mark the open page in a menu.

Return value

Returns ?Core\Router\Match. A Core\Router\Match with the declared name and the captures of the path. The result is null when no route matches this method and path. Such a request is still served like any other, because a match does not decide which code runs. A program with no #[Route] has no route table, so the result is also null.

Errors

  • LogicErrorThis program is not handling a request. For example, it is a command-line program, a scheduled script, a job worker or a test. The method throws this error and does not return null, because "no request arrived" and "no route matched" are different things.
  • RuntimeErrorA capture percent-decodes to bytes that are not UTF-8, so it cannot be a tainted string. The error names the capture and the position of the first byte that is not valid in a string.

Examples

The route that matched

<?nvs
// Shows how a program reads the route that the request matched, with its method and its captured value.
class Users
{
#[Core\Route(path: '/users/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $id): string
{
return 'one user';
}
}
// The address is `/users/42`. The server matches it against the routes before the program starts.
// `Core\Request::route()` returns that match.
?Core\Router\Match $matched = Core\Request::route();
if ($matched != null) {
// `is` checks which method the route belongs to. This prints "Users::show".
echo $matched->is(Users::show) ? 'Users::show' : 'another route', "\n";
// `{id}` is declared as `uint`, so the text "42" is the number 42. This prints "id 42".
echo 'id ', $matched->parameter('id') as uint, "\n";
}
Output
Users::show
id 42

An address no route matches

<?nvs
// Shows what `Core\Request::route` returns when no route matches the address.
class Users
{
#[Core\Route(path: '/users/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $id): string
{
return 'one user';
}
}
// The address is `/about`. No route has that path, so the result is `null`.
// This prints "no route for /about".
?Core\Router\Match $matched = Core\Request::route();
if ($matched == null) {
echo 'no route for ', Core\Request::path(), "\n";
}
Output
no route for /about

Marking the current page in a menu

<?nvs
// Shows how a page layout marks the menu entry for the page that is open.
class Shop
{
#[Core\Route(path: '/products', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public static function products(): string
{
return 'products';
}
#[Core\Route(path: '/orders', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public static function orders(): string
{
return 'orders';
}
}
// Each menu entry is the text to show and the method of its page.
// `Shop::products` is written without `()`, so nothing is called here.
array<callable> $menu = ['Products' => Shop::products, 'Orders' => Shop::orders];
// The address is `/orders`, so the route belongs to `Shop::orders`.
?Core\Router\Match $matched = Core\Request::route();
// `is` checks whether the route belongs to the method of the entry.
// This prints " Products" and then "> Orders".
foreach ($menu as string $label => callable $route) {
bool $open = $matched != null && $matched->is($route);
echo $open ? '> ' : ' ', $label, "\n";
}
Output
Products
> Orders