Core\Router::urlAbsolute
Builds the full URL of one of your routes. Core\Router::urlAbsolute works like Core\Router::url: you give it the method that has the #[Route], such as Articles::show, and an array of values. The result has the origin of your site in front, for example https://www.example.com/articles/42. A route with no captures needs no values: Core\Router::urlAbsolute(Pages::home).
You need a full URL wherever the link is read outside your site: in an email, a sitemap, or a message to another service.
The origin is the origin setting of the [[app]] block in nvs.toml. It never comes from the Host header of a request, so a visitor cannot change the site that your links point to. If no origin is set, the call throws a RuntimeError.
The examples show a link with and without the origin, a sitemap, and a link in an email.
Parameters
$routecallableThe method that has the#[Route], written as a method reference such asShop::show. The link does not call the method. So the method may be an instance method or a private method. A method reference written directly in the call is checked when the program compiles. A method without a#[Route]is a compile error.$paramsarray<mixed>default []The path's captures by name, plus any query parameters. A key written directly in the code must be a capture or a declared#[Query]parameter. Any other key is a compile error. A value written directly in the code must have a type that its parameter can take, or the program does not compile. ACore\Time\Datefor auintcapture is an example. Astringvalue is always allowed.urlandurlAbsoluteuse[]when you leave it out.
Return value
Returns string. The full URL, for example https://example.test/users/42?page=2. There is exactly one / between the origin and the path.
Errors
RuntimeErrorFor every case whereurlthrows an error. When nooriginis set for the program innvs.toml.
Examples
The examples below run with this configuration file.
# `Core\Router::urlAbsolute` puts this origin in front of each link.# `root = "."` means the programs in this directory use this block.[[app]]root = "."origin = "https://www.example.com"A full link to one page
<?nvs// Shows the difference between a link with and without the origin of the site.
class Articles{ #[Core\Route(path: '/articles/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id): string { return 'one article'; }}
// `Core\Router::url` returns only the path. This prints `/articles/42`.echo Core\Router::url(Articles::show, ['id' => 42]), "\n";
// `Core\Router::urlAbsolute` puts the `origin` from `nvs.toml` in front.// This prints `https://www.example.com/articles/42`.echo Core\Router::urlAbsolute(Articles::show, ['id' => 42]), "\n";/articles/42https://www.example.com/articles/42The pages of a sitemap
<?nvs// Shows how to write the entries of a sitemap. A sitemap must contain full URLs.
class Pages{ #[Core\Route(path: '/', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function home(): string { return 'home'; }
#[Core\Route(path: '/help/{topic}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function help(string $topic): string { return 'one help page'; }}
echo "<urlset>\n";
// The home page has no captures, so it needs no values.// Its path is `/`, so this link is `https://www.example.com/`.echo ' <url><loc>', Core\Router::urlAbsolute(Pages::home), "</loc></url>\n";
// Each topic is percent-encoded, so "gift cards" becomes `gift%20cards`.array<string> $topics = ['shipping', 'returns', 'gift cards'];foreach ($topics as string $topic) { string $link = Core\Router::urlAbsolute(Pages::help, ['topic' => $topic]); echo ' <url><loc>', $link, "</loc></url>\n";}
echo "</urlset>\n";<urlset> <url><loc>https://www.example.com/</loc></url> <url><loc>https://www.example.com/help/shipping</loc></url> <url><loc>https://www.example.com/help/returns</loc></url> <url><loc>https://www.example.com/help/gift%20cards</loc></url></urlset>A link in an email
<?nvs// Shows how to put a link to an order in an email. An email needs the full URL.
class Orders{ #[Core\Route(path: '/orders/{id}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public function show(uint $id, #[Core\Query] string $from = ''): string { return 'one order'; }}
uint $order = 1042;string $customer = 'Ana';
// `from` is not a capture of the path, so it goes after the `?`.// The link is `https://www.example.com/orders/1042?from=email`.string $link = Core\Router::urlAbsolute(Orders::show, ['id' => $order, 'from' => 'email']);
echo 'Hello ', $customer, ",\n\n";echo 'Your order ', $order, " is on its way.\n";echo 'You can follow it here: ', $link, "\n";Hello Ana,
Your order 1042 is on its way.You can follow it here: https://www.example.com/orders/1042?from=email