Generating an absolute node URL from its node id I think the question is self explanatory. I got a node ID and I need to create its absolute URL somehow. I can do this but (I think) this won't work every where in the site:

&lt;a href="?q=node/node-id"&gt;Node Title&lt;/a&gt;


Please correct me if I am wrong.

Is there a function that I can give the node id and returns me back the absolute path of it?
Drupal 8
$nid = 5;
// Or $nid = $node-&gt;id();
$node_title = 'Node Title'
// Or $node-&gt;getTitle();

Create absolute URL:
// Will output http://example.com/path-to-my-node
$options = ['absolute' =&gt; TRUE];
$url_object = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' =&gt; $nid], $options);
// In Twig use {{ url_object }}.
// In PHP (outside a render array) use $url_object-&gt;toString().

Create absolute link object (and inner span):
// Will output &lt;a href=&quot;http://example.com/path-to-my-node&quot; class=&quot;this-class&quot;&gt;&lt;span&gt;Node Title&lt;/span&gt;&lt;/a&gt;
$options = ['absolute' =&gt; TRUE, 'attributes' =&gt; ['class' =&gt; 'this-class']];
$node_title = \Drupal\Core\Render\Markup::create('&lt;span&gt;' . $node_title . '&lt;/span&gt;');
$link_object = \Drupal\Core\Link::createFromRoute($node_title, 'entity.node.canonical', ['node' =&gt; $nid], $options);
// In Twig use {{ link_object }}.
// In PHP (outside a render array) use $link_object-&gt;toString().