Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Get the current page URI on Drupal 8

By editor , 19 November, 2015
Question

How to get the current page uri /url / path / link on Drupal 8 (programmatically) ?

Here an example to get the current page path programmatically in Drupal 8

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

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

Drupal provide several way to do the same things, You can also use following syntax.
$path = \Drupal::request()->attributes->get('_system_path');
$current_uri = \Drupal::request()->getRequestUri();
$current_path = \Drupal::service('path.current')->getPath();
$result = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);

Get the url of the request

This return the url displayed on the browser.
$page = \Drupal::request()->getRequestUri();

Tags

  • Drupal 8
  • Code
Drupal 8 Url Tips. Get Name, Path, Args ...

Comments

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer