Get args from URL in Drupal 8

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

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

Example: 

    $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];

 

 

Tags

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.