Core\Uri::path
$uri->path(): stringReturns the path of an address. The path is the part after the host and before the ? or the #, such as /docs/start in https://example.com/docs/start?page=2.
$uri->path() returns the path as it was written. Escapes such as %20 stay in it, and so do parts such as /./ and /../. Use Core\Uri::decodeComponent on one part of the path to read its text.
The result is never null, because every address has a path. An address with nothing after the host, such as https://example.com, has the path "".
The examples below read a path, show that it is returned as it was written, and choose which page a server shows for a request.
Return value
Returns string. The path. It is never null. An address with no path, such as https://example.com, returns "".
Examples
The page in a link
<?nvs// Shows how to read the path of an address.
var $link = Core\Uri::parse('https://example.com/docs/start?page=2#install');
// The path stops before the `?`. This prints /docs/start.echo $link->path(), "\n";
// A link without a host has a path too. This prints /account/orders.echo Core\Uri::parse('/account/orders?sort=date')->path(), "\n";/docs/start/account/ordersThe path as written
<?nvs// Shows that `path` returns the path exactly as it was written.
// Escapes and `..` parts stay in the path. This prints /files/../my%20notes.txt.echo Core\Uri::parse('https://example.com/files/../my%20notes.txt')->path(), "\n";
// An address with nothing after the host has an empty path. This prints "".echo "\"", Core\Uri::parse('https://example.com')->path(), "\"\n";
// An address without `//` has a path too. This prints someone@example.com.echo Core\Uri::parse('mailto:someone@example.com')->path(), "\n";/files/../my%20notes.txt""someone@example.comChoose a page by the path
<?nvs// Shows how a server chooses which page to show for the path of a request.
class Router{ public static function page(string $address): string { string $path = Core\Uri::parse($address)->path(); if ($path == '' || $path == '/') { return 'home page'; } // `/products/42` splits into "", "products" and "42". array<string> $parts = Core\Str::split($path, '/'); if (Core\Arr::count($parts) == 3 && $parts[1] == 'products') { return 'product ' . $parts[2]; } return 'not found'; }}
// In a real program, the address comes from the request.array<string> $requests = [ 'https://shop.example.com/', 'https://shop.example.com/products/42?color=red', 'https://shop.example.com/admin',];
// This prints home page, product 42 and not found.foreach ($requests as string $request) { echo Router::page($request), "\n";}home pageproduct 42not found