Skip to content

Core\Router::match

Finds the route that an HTTP method and a path belong to. Core\Router::match checks them against every #[Route] in your program and returns a Core\Router\Match. The match has the route and its captures. $match->is(Articles::show) checks which method the route belongs to. The match does not run the route's handler.

The captures are decoded, and each one has the type of its handler parameter. For example, {id} is a number when the handler takes uint $id. If no route has that method and that path, the result is null. A capture whose escapes are not valid UTF-8 throws a RuntimeError.

A request that is being served already has its match: use Core\Request::route() for that. Core\Router::match is for a path that your program chose, for example a link in a menu.

The examples show a match and its captures, a path that no route matches, and a check of every link in a menu.

Parameters

  • $methodCore\Http\MethodThe HTTP method. A route declared for Get does not match a Post.
  • $pathstringThe path, with no query string. A mount prefix is not removed from it.

Return value

Returns ?Core\Router\Match. A Core\Router\Match with the route's name and its captures. Each capture is decoded and has the type of its handler parameter. The result is null when no route has this method and this path, or when the program has no routes.

Errors

  • RuntimeErrorA capture decodes to bytes that are not valid UTF-8. The message names the capture and the position of the first bad byte.

Examples

Find the route for a path

<?nvs
// Shows how to find the route that a method and a path belong to.
class 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';
}
}
// `Core\Router::match` checks the path against every `#[Route]` in the program.
// The result is a `Core\Router\Match` with the route and its captures.
?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Get, '/articles/42');
if ($found != null) {
// `is` checks which method the route belongs to. This prints `Articles::show`.
echo $found->is(Articles::show) ? 'Articles::show' : 'another route', "\n";
// `{id}` is a `uint` in `show`, so the capture is the number 42.
echo $found->parameter('id'), "\n";
}
Output
Articles::show
42

A path no route matches

<?nvs
// Shows what `Core\Router::match` returns when no route matches.
class Contact
{
#[Core\Route(path: '/contact', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function form(): string
{
return 'the form';
}
}
// The path exists, but only for `Get`. A `Post` to it does not match,
// so the result is `null`.
?Core\Router\Match $sent = Core\Router::match(Core\Http\Method::Post, '/contact');
echo $sent == null ? 'post: no route' : 'post: matched', "\n";
// No route has this path at all, so the result is also `null`.
?Core\Router\Match $missing = Core\Router::match(Core\Http\Method::Get, '/contact-us');
echo $missing == null ? 'get /contact-us: no route' : 'get /contact-us: matched', "\n";
// `Core\Router::methodsFor` tells the two cases apart.
// This prints 1 for `/contact` and 0 for `/contact-us`.
echo Core\Arr::count(Core\Router::methodsFor('/contact')), "\n";
echo Core\Arr::count(Core\Router::methodsFor('/contact-us')), "\n";
Output
post: no route
get /contact-us: no route
1
0

Check the links in a menu

<?nvs
// Shows how to check that every link in a site menu opens a page of this program.
class Shop
{
#[Core\Route(path: '/', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function home(): string
{
return 'home';
}
#[Core\Route(path: '/products/{slug}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function product(string $slug): string
{
return 'one product';
}
#[Core\Route(path: '/cart', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function cart(): string
{
return 'the cart';
}
}
// The menu is often written by hand, so a link can point at a page that does not exist.
array<string> $menu = ['/', '/products/red-shoes', '/basket', '/cart'];
// A deploy script or a test runs this loop before the menu goes live.
// Each link prints `ok` when a route matches it, or `BROKEN` when no route does.
uint $broken = 0;
foreach ($menu as string $link) {
?Core\Router\Match $page = Core\Router::match(Core\Http\Method::Get, $link);
if ($page == null) {
echo $link, ' -> BROKEN', "\n";
$broken = $broken + 1;
} else {
echo $link, ' -> ok', "\n";
}
}
echo $broken, ' broken link(s)', "\n";
Output
/ -> ok
/products/red-shoes -> ok
/basket -> BROKEN
/cart -> ok
1 broken link(s)