How do I create a HTML link from an internal path using the core Link class?
Getting the route can be tricky. Sometimes I'll just do a code search for the path.

Routes are defined in [MODULE].routing.yml files. You can often look for a route you need if you can guess the module that supplies it. For example, the user login page is provided by the user module, so I look in user.routing.yml, in which I find this for the user login route:

user.login:
  path: '/user/login'
  defaults:
    _form: '\Drupal\user\Form\UserLoginForm'
    _title: 'Log in'
  requirements:
    _user_is_logged_in: 'FALSE'
  options:
    _maintenance_access: TRUE


The route name here is user.login. Now, that route name can be used to do the following:

$url = Url::fromRoute('user.login');
// Get the URL string:
$url_string = $url-&gt;toString();
// Build a link:
$link = Link::fromTextAndUrl(t('Log In'), $url);
// Get the link string:
$link_string = $link-&gt;toString();


Note that $link-&gt;toString() returns an object of type Drupal\Core\GeneratedLink. This class has the magic method GeneratedLink::__toString(). This magic method is invoked when the object is treated like a string. That means you can treat the result of $link-&gt;toString() as if it were a string, even though it's an object. So in context where you can print values, this will work:

print Link::fromTextAndUrl(t('mylink'), Url::fromRoute('user.login'))-&gt;toString();