How to get the base URL of a site
You can get the hostname, "drupal8.local", directly from the getHost() request:

$host = \Drupal::request()-&gt;getHost();


In some cases you might want to get the schema as well, fx https://drupal8.local:

$host = \Drupal::request()-&gt;getSchemeAndHttpHost();