Skip to content

Core\Router\Match::is

$match->is(callable $route): bool$match->is(
    callable $route,
): bool

Checks whether the route that a path matched belongs to a method. You write the method the same way as for Core\Router::url, without (): $match->is(Shop::product). Nothing is called. The result is true when the route belongs to that method, and false otherwise.

A method can have more than one #[Route], for example a GET and a POST route with the same path. is returns true for each of these routes.

You get a match from Core\Request::route() for the request that is being served, or from Core\Router::match for a path that you choose.

The method can also be in a variable, for example an item of an array<callable> menu. A method that you write in the call must have a #[Route], or the program does not compile. A variable whose method has no #[Route] throws a RuntimeError.

The examples check the route of a served request, use a method in a variable, and call the right method for each route.

Parameters

  • $routecallableA method reference, such as Shop::show. A method that you write here must have a #[Route], or the program does not compile.

Return value

Returns bool. true when the route belongs to this method, and false otherwise. A method with two #[Route]s, one for each HTTP method, gives true for both.

Errors

  • RuntimeErrorWhen $route is a variable and its method has no #[Route].

Examples

The route of a request

<?nvs
// Shows how to check which method the route of the current request belongs to.
class Articles
{
#[Core\Route(path: '/articles', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function list(): string
{
return 'all articles';
}
#[Core\Route(path: '/articles/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function show(uint $id): string
{
return 'one article';
}
}
// The request is `GET /articles/42`. `Core\Request::route()` returns its match.
?Core\Router\Match $matched = Core\Request::route();
if ($matched != null) {
// `Articles::show` is written without `()`, so nothing is called.
// The route belongs to `show`, so this prints `true` and then `false`.
echo $matched->is(Articles::show) ? 'true' : 'false', "\n";
echo $matched->is(Articles::list) ? 'true' : 'false', "\n";
}
Output
true
false

A method from a variable

<?nvs
// Shows `is` with a method that is in a variable, and a method with two routes.
class Feedback
{
// One method has two routes: `GET` shows the form and `POST` sends it.
#[Core\Route(path: '/feedback', method: Core\Http\Method::Get)]
#[Core\Route(path: '/feedback', method: Core\Http\Method::Post)]
#[Core\Access(allow: Core\Audience::Public)]
public static function form(): string
{
return 'the form';
}
#[Core\Route(path: '/help', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public static function help(): string
{
return 'the help page';
}
}
// A variable of type `callable` can contain a method. Nothing is called here.
callable $form = Feedback::form;
callable $help = Feedback::help;
?Core\Router\Match $sent = Core\Router::match(Core\Http\Method::Post, '/feedback');
if ($sent != null) {
// The `POST` route also belongs to `form`. This prints `true`.
echo $sent->is($form) ? 'true' : 'false', "\n";
// The route does not belong to `help`. This prints `false`.
echo $sent->is($help) ? 'true' : 'false', "\n";
}
Output
true
false

Call the method for each route

<?nvs
// Shows how a program calls the method of the matched route itself, without `Core\Router::dispatch`.
class Shop
{
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function home(): string
{
return 'Welcome to the shop';
}
#[Core\Route(path: '/products/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function product(uint $id): string
{
return 'Product number ' . ($id as string);
}
}
// The request is `GET /products/7`.
?Core\Router\Match $matched = Core\Request::route();
if ($matched == null) {
echo 'Page not found', "\n";
} else {
var $shop = new Shop();
// Each `is` checks one method. The first one that returns `true` is called.
// This prints `Product number 7`.
if ($matched->is(Shop::home)) {
echo $shop->home(), "\n";
} elseif ($matched->is(Shop::product)) {
echo $shop->product($matched->parameter('id') as uint), "\n";
} else {
echo 'Page not found', "\n";
}
}
Output
Product number 7