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
$host = \Drupal::request()->getSchemeAndHttpHost();


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.