Drupal 8: How to Get the Site Hostname

Submitted by admin on Sun, 03/04/2018 - 20:19

\Drupal::request()->getHost()

This one doesn't include the URL that begins with https://〜, therefore you may want to write it as follows:
$hostname = sprintf('https://%s/', \Drupal::request()->getHost());

Tags