Get args from URL in Drupal 8 | Drupal 8

Get args from URL in Drupal 8

Submitted by editor on Mon, 12/14/2015 - 16:00

How to get arguments (parameters) from url in Drupal 8 ?
Like $path_args = arg(); on Drupal 7.


    $current_url = Url::fromRoute('<current>');
    $path = $current_url->toString();
    //OR of you want to get the url without language prefix
    $path = $current_url->getInternalPath();
    $path_args = explode('/', $path);

Method 2: (Get first argument from url on Drupal 8)

$request = \Drupal::request();
$current_path = $request->getPathInfo();
$path_args = explode('/', $current_path);
$first_argument = $path_args[1];





Add new comment

Plain text

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