Get the current page URI programmatically on Drupal 8?

drupal 8 uri path

<?php 

$current_url = Url::fromRoute('<current>');
$path = $current_url->toString();

//Examples for the page /en/user/login:
$current_url->toString();    // /en/user/login
$current_url->getInternalPath();    // user/login
$path = $current_url->getRouteName();   // <current>

//Drupal syntax.
$path = \Drupal::request()->attributes->get('_system_path');

// Include a query string
$current_uri = \Drupal::request()->getRequestUri();

// For the current raw path (the un-aliased Drupal path):
$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

//Get the url of the request for displayed on the browser.
$page = \Drupal::request()->getRequestUri();

?>

 

Tags