Core\Router\Match::parameter
$match->parameter(
string $name,
): ?tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|ParsesReturns one value that a route captured from the path, found by its name. You write the name without the braces: parameter("id") for {id}. The result is the same value that parameters() has under that key.
The value is already percent-decoded, and it has the type of its handler parameter. A text capture is tainted. That means it came from the visitor, so you must check it before you use it.
The result is null when the route has no capture with that name. It is also null when an optional capture such as {page?} is not in the path, so ?? can give a default.
Inside Core\Router::dispatch, the match also has each #[Core\Query] value under its parameter's name. So a gate's allows method can read a query value. The match from Core\Request::route() or Core\Router::match() has no query values, and parameter returns null for them.
The examples read one capture and give an optional capture a default. They also find the record that the path names, and read a query value in a gate.
Parameters
$namestringThe capture's name as the route's path writes it, without the braces, or the name of a#[Core\Query]parameter without the$.
Return value
Returns ?tainted string|int|uint|decimal|bool|Core\Uuid|Core\Time\Date|Core\Time\TimeOfDay|Core\Time\Instant|Core\Time\DateTime|Parses. The value, with the type of its handler parameter. A string value is tainted: it is text from outside the program, which you must check before you use it. The result is null when the route has no capture or query value with this name. It is also null when an optional {name?} is not in the path. A query value read from the match of Core\Request::route() or Core\Router::match() is null. Only Core\Router::dispatch reads the query string.
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
Read one capture
<?nvs// Shows how to read one value that the route captured from the path.
class Profiles{ #[Core\Route(path: '/users/{username}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(string $username): string { return 'one profile'; }}
// The request is `GET /users/alice`.?Core\Router\Match $matched = Core\Request::route();if ($matched != null) { // `parameter("username")` returns the capture `{username}`. This prints `alice`. echo $matched->parameter('username') as string, "\n";
// The route has no capture called `id`, so the result is `null`. // This prints `no id`. echo $matched->parameter('id') == null ? 'no id' : 'has id', "\n";}aliceno idA default for an optional capture
<?nvs// Shows how to give an optional capture a default value.
class Search{ #[Core\Route(path: '/search/{page?}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function results(uint $page = 1): string { return 'the results'; }}
array<string> $paths = ['/search', '/search/4'];foreach ($paths as string $path) { ?Core\Router\Match $found = Core\Router::match(Core\Http\Method::Get, $path); if ($found != null) { // `{page?}` is optional. When the path has no page, `parameter("page")` is `null`, // and `??` gives 1. This prints page 1 for `/search` and page 4 for `/search/4`. uint $page = ($found->parameter('page') ?? 1) as uint; echo $path, ' -> page ', $page, "\n"; }}/search -> page 1/search/4 -> page 4Find the record the path names
<?nvs// Shows how to find the record that the path names, as a page for one invoice does.
class Invoices{ #[Core\Route(path: '/invoices/{number}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $number): string { return 'one invoice'; }}
// A small list of invoices. A real program reads them from a database.array<string> $invoices = [1001 => 'Web hosting, 12.00 EUR', 1002 => 'Domain renewal, 9.50 EUR'];
// The request is `GET /invoices/1002`.?Core\Router\Match $matched = Core\Request::route();if ($matched != null) { // `{number}` is a `uint` in `show`, so the capture is already a number. uint $number = $matched->parameter('number') as uint; // This prints `Invoice 1002: Domain renewal, 9.50 EUR`. echo 'Invoice ', $number, ': ', $invoices[$number] ?? 'not found', "\n";}Invoice 1002: Domain renewal, 9.50 EURRead a query value in the gate
<?nvs// Shows how a gate reads a query value through the match, before the route method runs.use Core\Http\Method;
class Reports{ #[Core\Route(path: '/reports', method: Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function list(#[Core\Query] uint $limit = 10): void { // `Core\Request::route()` returns a match that has no query values. // In the route method, `parameter("limit")` on that match is `null`. This prints `null`. ?Core\Router\Match $route = Core\Request::route(); echo ' route: ', $route?->parameter('limit') == null ? 'null' : 'set'; }}
class Gate implements Core\Router\Gate{ // Inside `Core\Router::dispatch`, the match has each query value under its parameter's name. // This gate allows at most 100 rows. public function allows(Core\Router\Match $route): bool { uint $limit = $route->parameter('limit') as uint; echo 'limit ', $limit; return $limit <= 100; }
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 { // `limit` is missing, so the gate reads the default value 10. This prints `200`. var $default = Core\Test::request(Method::Get, '/reports'); echo $default->status(), ' ', $default->body(), "\n";
// The gate reads 500 and returns `false`. This prints `403`. var $large = Core\Test::request(Method::Get, '/reports?limit=500'); echo $large->status(), ' ', $large->body(), "\n";}200 limit 10 route: null403 limit 500, not allowed