Here are some examples of creating links in Drupal 8. Note that $this->t('some text') is available within blocks that extend BlockBase. If you copy these to another class that doesn't have it or use these in a .module file, you may need to change this to t() 1.
Basic Link to a node:
$node = Node::load($nid);
$build['node_link'] = $node->toLink()->toRenderable();
This creates a render array like this:
$link = [
'#type' => 'link',
'#url' => $url_object,
'#title' => 'Title of Node',
];
You can create the render array without loading the node this way:
$url_object = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link = [
'#type' => 'link',
'#url' => $url_object,
'#title' => $this->t('Read More'),
];
Or using the core Link class:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
If you want to use markup in the text of your link, you can't just put a string in. You need to use a render array element:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
$link_text = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => $this->t('Load More'),
];
$link = Link::fromTextAndUrl($link_text, $url);
To create an absolute link, you add this option to the URL, not the link:
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], ['absolute' => TRUE]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To add a class to your link, you also need to add this to the URL, not the link:
$options = [
'attributes' => [
'class' => [
'read-more-link',
],
],
];
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To add a query string to your link, you also need to this to the URL, not the link:
$options = [
'query' => [
'car' => 'BMW',
'model' => 'mini-cooper',
],
'attributes' => [
'class' => [
'read-more-link',
],
],
];
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
To set the link to open in a new window with target = _blank:
$options = [
'attributes' => [
'target' => '_blank'
],
];
$url = Url::fromRoute('entity.media.edit_form', ['media' => $entity->id()], $options);
$link = Link::fromTextAndUrl(t('Edit'), $url);
$form['entity']['edit_link'] = $link->toRenderable();
Here's a link to a taxonomy term page:
$url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]);
$link = Link::fromTextAndUrl($this->t('Read more'), $url);
$build['read_more'] = $link->toRenderable();
Here's a link to node edit page:
$url = Url::fromRoute('entity.node.edit_form', ['node' => $nid]);
$link = Link::fromTextAndUrl($this->t('Edit'), $url);
$build['read_more'] = $link->toRenderable();
To create an external link:
$url = Url::fromUri('http://www.example.com/');
$link = Link::fromTextAndUrl($this->t('Vist this example site'), $url);
$build['external_link'] = $link->toRenderable();
Link to homepage:
$url = Url::fromRoute('<front>');
$link = Link::fromTextAndUrl($this->t('Home'), $url);
$build['homepage_link'] = $link->toRenderable();
Note that on any of these url objects, you can get the url as a string by calling:
$url->toString();
For example:
$url_string = Url::fromRoute('<front>')->toString();