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();
Let's add some more options
Get the current route name:
$current_route = \Drupal::routeMatch()->getRouteName();
Get current query parameters (upscaled):
$current_parameters = \Drupal::routeMatch()->getParameters();
Get current URL as an object of type Drupal\Core\Url, which requires the query parameters to be an array of values rather than upscaled objects:
use Drupal\Core\Url; $url = Url::fromRoute(\Drupal::routeMatch()->getRouteName(), \Drupal::routeMatch()->getRawParameters()->all());
And for the grand finale, get the full current URL, not just the path (Drupal 9):
Url::fromRoute('<current>', array(), array('absolute' => 'true'))->toString();