Oct 16, 2020 in Drupal 8, Snippets

This snippet sums op the possibilities of getting the current url path alias.

// aliased path /blog/get-current-path-alias
$url = \Drupal\Core\Url::fromRoute('<current>');
// raw un-aliased path /node/1
$current_path = \Drupal::service('path.current')->getPath();

Add the hostname

To get the current full path here's how to get the hostname:

// Returns https://stefvanlooveren.me
$host = \Drupal::request()->getSchemeAndHttpHost();