May 28, 2019 in Drupal 8, Snippets

How do you get the base url of a drupal 8 website programmatically?

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

Or

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