Core\Router\Match::parameters
$match->parameters(): array<tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|Parses>Returns every value that a route captured from the path, as an array. Each key is the name in the route's path, such as id for {id}. The values are in the order of the path.
Each value is already percent-decoded, so %20 in the path is a space. Each value also has the type of its handler parameter: {id} read by uint $id is a number. A text capture is tainted. That means it came from the visitor, so you must check it before you use it.
An optional capture that the path does not have is not in the array. A route with no captures returns an empty array.
Inside Core\Router::dispatch, the array also has each #[Core\Query] value, under its parameter's name. A query value with a default other than null is always in the array. A query value with the default null is left out when the request does not have it. The match from Core\Request::route() or Core\Router::match() has only the path captures.
The examples read the captures of a path, routes with no captures and a log line. The last one shows what a gate sees.
Return value
Returns array<tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|Parses>. An array of the captures. Each value is already percent-decoded, so %20 is a space. Each value has the type of its handler parameter: {id} read by uint $id is a number. A string capture is tainted: it is text from outside the program, which you must check before you use it. An optional capture that the path does not have is not in the array. A query key that is absent and has the default null is not in the array either. The match from Core\Request::route() or Core\Router::match() has no query values. A route with no captures returns an empty array.
Changelog
| Version | Change |
|---|---|
0.0.6 | Inside Core\Router::dispatch, Core\Router\Match::parameter and parameters also return each converted #[Core\Query] value. So Gate::allows can check a record that a query value loaded. |
0.0.6 | Core\Router\Match reads the values of a route with parameters() and parameter($name). The old names were params and param. |
Examples
Every capture of a path
<?nvs// Shows how to read every value that a route captured from the path.
class Blog{ #[Core\Route(path: '/blog/{year}/{slug}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function post(uint $year, string $slug): string { return 'one post'; }}
?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Get, '/blog/2026/hello%20world');if ($found != null) { // `parameters()` returns an array. Each key is the name in the route's path. // `%20` is already decoded, so this prints `year: 2026` and `slug: hello world`. foreach ($found->parameters() as tainted string $name => mixed $value) { echo $name, ': ', $value as string, "\n"; }}year: 2026slug: hello worldRoutes with no captures
<?nvs// Shows that `parameters()` returns an empty array when the path has no captures.
class Pages{ #[Core\Route(path: '/about', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function about(): string { return 'about us'; }
#[Core\Route(path: '/archive/{page?}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function archive(uint $page = 1): string { return 'the archive'; }}
// `/about` has no captures, so this prints 0.?Core\Router\Match $about = Core\Router::match(Core\Http\Method::Get, '/about');if ($about != null) { echo Core\Arr::count($about->parameters()), "\n";}
// `{page?}` is optional. The path `/archive` has no page, so this prints 0 too.?Core\Router\Match $first = Core\Router::match(Core\Http\Method::Get, '/archive');if ($first != null) { echo Core\Arr::count($first->parameters()), "\n";}
// The path `/archive/3` has a page, so this prints 1.?Core\Router\Match $third = Core\Router::match(Core\Http\Method::Get, '/archive/3');if ($third != null) { echo Core\Arr::count($third->parameters()), "\n";}001A log line for a request
<?nvs// Shows how to write one log line with the HTTP method and every capture of the request.
class Orders{ #[Core\Route(path: '/orders/{order}/items/{item}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function item(uint $order, uint $item): string { return 'one item'; }}
// The request is `GET /orders/1042/items/3`.?Core\Router\Match $matched = Core\Request::route();if ($matched != null) { // `Core\Http::methodName` converts the method to text. // The loop adds each capture as `name=value`. This prints `GET order=1042 item=3`. echo Core\Http::methodName($matched->method()); foreach ($matched->parameters() as tainted string $name => mixed $value) { echo ' ', $name, '=', $value as string; } echo "\n";}GET order=1042 item=3Captures and query values in the gate
<?nvs// Shows that a gate sees the path captures and the query values together in `parameters()`.use Core\Http\Method;
class Shops{ #[Core\Route(path: '/shops/{shop}', method: Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function products(uint $shop, #[Core\Query] uint $page = 1, #[Core\Query] ?string $tag = null): void { // `Core\Request::route()` returns a match that has only the path captures. // This route has one capture, `shop`, so this prints `1 value` for each request. ?Core\Router\Match $route = Core\Request::route(); if ($route != null) { echo Core\Arr::count($route->parameters()), " value\n"; } }}
class Gate implements Core\Router\Gate{ // Inside `Core\Router::dispatch`, `parameters()` has each capture and each query value. // A missing `?string $tag = null` value is not in the array. public function allows(Core\Router\Match $route): bool { echo 'gate:'; foreach ($route->parameters() as tainted string $name => mixed $value) { echo ' ', $name, '=', $value as string; } echo "\n"; return true; }
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void { echo 'Not allowed.'; }
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 request has no `page` and no `tag`. `page` gets its default value 1, // and `tag` is not in the array. The gate prints `gate: shop=7 page=1`. echo Core\Test::request(Method::Get, '/shops/7')->body();
// The gate prints `gate: shop=7 page=3 tag=red`. echo Core\Test::request(Method::Get, '/shops/7?page=3&tag=red')->body();}gate: shop=7 page=11 valuegate: shop=7 page=3 tag=red1 value