Skip to content

Core\Request::path

Returns the path of the request that your program is answering, such as /orders/1042. The path is the part of the address after the host name and before the ?.

The server can serve your program under a prefix, such as /shop. The server removes that prefix before your program runs, so path returns /cart for the address /shop/cart. Your program then reads the same paths under any prefix. Core\Request::mount returns the prefix that was removed.

The path is returned as the client sent it. A space is still written as %20. The result is 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 path throws a LogicError there.

The examples below show a path with a %20 in it, a program served under a prefix, and a program that picks a page by its path.

Return value

Returns tainted string. The rest of the path after the mount prefix, percent-encoded as it arrived. It is tainted. mount() returns the prefix that was removed.

Errors

  • LogicErrorThis program is not handling a request.

Examples

A path as the client sent it

<?nvs
// Shows how to read the path of a request.
// The client asked for a file with a space in its name.
// This prints "/docs/price%20list.pdf". The space is still written as `%20`.
tainted string $path = Core\Request::path();
echo $path, "\n";
Output
/docs/price%20list.pdf

A program under a prefix

<?nvs
// Shows that the path does not contain the prefix that the server serves the program under.
// The client asked for `/shop/cart`. The server serves this program under `/shop`.
// This prints "/cart".
echo Core\Request::path(), "\n";
// `Core\Request::mount()` returns the prefix. This prints "/shop".
echo Core\Request::mount()->prefix(), "\n";
Output
/cart
/shop

Pick a page by its path

<?nvs
// Shows a small program that picks the page to show by the path of the request.
tainted string $path = Core\Request::path();
// Each known path has its own page. Any other path gets a "not found" page.
// The client asked for `/about`, so this prints "About our shop".
if ($path == '/') {
echo "Welcome to our shop\n";
} elseif ($path == '/about') {
echo "About our shop\n";
} else {
echo "404: page not found\n";
}
Output
About our shop