Skip to content

Core\Request\Mount::captures

$mount->captures(): array<tainted string>

Returns the parts of the mount's prefix that a pattern matched, as an array of strings.

A server can use one pattern to serve many folders, one for each customer. A pattern such as /{1:lower} serves the folder Acme at /acme and the folder Globex at /globex. When a request arrives at /acme, captures() returns ["Acme"]. The first part, {1}, is captures()[0], the second part is captures()[1], and so on. A mount without a pattern returns an empty array.

The strings are tainted. A tainted string came from the client, so Novis makes you check it before you use it in a query or a file path.

In plain words: the address is a door, and the capture is the name written on it. The client picks which door to knock on, so your program checks the name before it trusts it.

The examples below read the parts of a pattern in order, handle a mount without a pattern, and pick the language of a page from the address.

Return value

Returns array<tainted string>. The captures as tainted string values. The array is empty for a mount whose prefix has no glob. They are tainted because the client chose which mount serves the request. Before you use one in a query or a path, check it as you check any other value from a request.

Examples

The parts in order

<?nvs
// Shows how a program reads each part of its prefix that a pattern matched.
// The server serves this program at `/acme/eu`, through the pattern `/{1:lower}/{2}`.
// `{1}` matched the folder "Acme" and `{2}` matched the folder "eu".
Core\Request\Mount $mount = Core\Request::mount();
array<tainted string> $parts = $mount->captures();
// `{1}` is `captures()[0]`. This prints "customer: Acme".
echo 'customer: ', $parts[0], "\n";
// `{2}` is `captures()[1]`. This prints "region: eu".
echo 'region: ', $parts[1], "\n";
// This prints "parts: 2".
echo 'parts: ', Core\Arr::count($parts), "\n";
Output
customer: Acme
region: eu
parts: 2

A mount without a pattern

<?nvs
// Shows what `captures()` returns when the mount has no pattern.
// The server serves this program at `/shop`. That prefix has no pattern,
// so `captures()` returns an empty array. This prints "parts: 0".
array<tainted string> $parts = Core\Request::mount()->captures();
echo 'parts: ', Core\Arr::count($parts), "\n";
// `??` gives a default value when index 0 does not exist.
// This prints "customer: main".
tainted string $customer = $parts[0] ?? 'main';
echo 'customer: ', $customer, "\n";
Output
parts: 0
customer: main

The language from the address

<?nvs
// Shows how a program picks the language of a page from its address.
// The server serves one folder for each language, through the pattern `/{1}`.
// The client asked for `/de/welcome`, so `captures()[0]` is "de".
tainted string $language = Core\Request::mount()->captures()[0];
// The program knows these languages. A language it does not know gets English.
array<string> $greetings = ['en' => 'Welcome', 'de' => 'Willkommen', 'fr' => 'Bienvenue'];
string $greeting = $greetings[$language] ?? $greetings['en'];
// This prints "Willkommen".
echo $greeting, "\n";
Output
Willkommen