<?php
// To get the base url
$host = \Drupal::request()->getSchemeAndHttpHost();
//https://www.local.com
$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();
?>
- Add new comment
- 20355 views
Comment
To get the Base URL: $host =…
Submitted by TS (not verified) on Thu, 10/28/2021 - 06:16
To get the Base URL:
$host = \Drupal::request()->getSchemeAndHttpHost();
//https://local.drupal.com
To get the current path Route
Submitted by TS (not verified) on Fri, 04/01/2022 - 12:09
//To get the current path Route
$current_path = \Drupal::service('path.current')->getPath();
$getRoute = \Drupal::routeMatch()->getRouteName();