Skip to main content

How to create an absolute URL from a route name

Posted on February 2, 2020

Create a Drupal\Core\Url object from the route name with the 'absolute' options set to TRUE.
Then call the toString() method to get the absolute url string.

$url_object = \Drupal\Core\Url::fromRoute($route_name, $route_parameters, ['absolute' => TRUE]);
$absolute_url = $url_object->toString();

For example, to get the absolute url of a node with id of 1 do the following

$url_object = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => 1], ['absolute' => TRUE]);
$absolute_url = $url_object->toString();