Skip to content

Core\Request::mount

Returns the mount that is serving this request, as a Core\Request\Mount. A mount is one entry in the server's configuration. It says which program answers the addresses that start with a prefix, such as /shop.

The server removes the prefix from the path before your program sees it. prefix() returns the removed text, so you can put it in front of the links you build. A mount at / removes nothing, and prefix() returns "".

A prefix can contain a pattern that matches many folders, one for each customer. captures() returns the parts that the pattern matched, in order. These 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.

A command-line program answers no request, so mount throws a LogicError there.

The examples below show a link built under the prefix, a program served at the root, and one program that serves many customers.

Return value

Returns Core\Request\Mount. A Core\Request\Mount. The result is never null, because every request reaches the program through a mount. A mount with no prefix and no glob gives the prefix "" and an empty array of captures.

Errors

  • LogicErrorThis program is not handling a request. For example, it is a command-line program, a scheduled script, a job worker or a test. None of these is mounted.

Examples

A link under the prefix

<?nvs
// Shows how a program builds its links when the server serves it under a prefix.
// The server serves this program at `/shop`. The client asked for `/shop/orders`.
Core\Request\Mount $mount = Core\Request::mount();
// `path()` returns the path without the prefix. This prints "/orders".
echo Core\Request::path(), "\n";
// `prefix()` returns the prefix that the server removed. This prints "/shop".
echo $mount->prefix(), "\n";
// Put the prefix in front of a link, so the link works under `/shop`.
// This prints "/shop/orders/1042".
echo $mount->prefix(), '/orders/1042', "\n";
Output
/orders
/shop
/shop/orders/1042

A program at the root

<?nvs
// Shows what `Core\Request::mount` returns when the server serves the program at `/`.
// A mount at `/` removes nothing from the path. `prefix()` returns "".
// This prints "prefix: ''".
Core\Request\Mount $mount = Core\Request::mount();
echo "prefix: '", $mount->prefix(), "'\n";
// This mount has no pattern, so `captures()` returns an empty array. This prints "captures: 0".
echo 'captures: ', Core\Arr::count($mount->captures()), "\n";
// The same code for a link works here too. This prints "/orders/1042".
echo $mount->prefix(), '/orders/1042', "\n";
Output
prefix: ''
captures: 0
/orders/1042

One program for many customers

<?nvs
// Shows how one program serves many customers, each under a prefix of its own.
// The server has one folder for each customer, such as `Acme` and `Globex`.
// The prefix `/{1:lower}` serves each folder at its own prefix, such as `/acme`.
// `captures()` returns the folder name that matched. Here it is "Acme".
Core\Request\Mount $mount = Core\Request::mount();
tainted string $customer = $mount->captures()[0];
// The program knows each customer and the database it uses.
array<string> $databases = ['Acme' => 'shop_acme', 'Globex' => 'shop_globex'];
// `??` gives "" for a name the program does not know.
// This prints "customer Acme uses shop_acme".
string $database = $databases[$customer] ?? '';
if ($database != '') {
echo 'customer ', $customer, ' uses ', $database, "\n";
} else {
echo "404: unknown customer\n";
}
Output
customer Acme uses shop_acme