0

Route from a custom module :

custom.route:
  path: /the/{id}/path

Hypothetical url : /the/53/path

The goal is the get the id value ("53").


So far, I managed to get the route name from the url with

$url = "/the/53/path";

$route_provider = \Drupal::service('router.route_provider');
$found_routes = $route_provider->getRoutesByPattern($url);

In $found_routes, I can find the "custom.route" route name.

Now, I need to find the parameters value, but I can't achieve it.


use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Returns the parameters of a given URL.
 *
 * @param string $url
 *   The URL.
 * @param string $route_name
 *   The name of the route.
 *
 * @return array
 *   An array of parameters for the URL.
 */
function getUrlParameters(string $url, string $route_name): array {
  // Create a URL object from the given URL string.
  $url_object = Url::fromUri('base:/' . ltrim($url, '/'));

  // Create a request object from the URL object.
  $request = \Drupal::requestStack()->getMasterRequest();
  $request = $request->duplicate();
  $request->setMethod('GET');
  $request->attributes->set('_route', $route_name);
  $url_object->mergeOptions(['query' => $request->query->all()]);
  $request->server->set('REQUEST_URI', $url_object->toString());

  // Use the route matcher to get the route matching the URL.
  /** @var \Drupal\Core\Routing\RouteMatchInterface $route_match */
  $route_match = RouteMatch::createFromRequest($request, \Drupal::service('router'));

  // Return the parameters of the URL.
  return $route_match->getParameters()->all();
}

2
  • 2
    What is your use case? I can't help but think there is likely a better way to do this
    – Jaypan
    Commented Feb 9, 2023 at 0:53
  • I create a controller page on a specific route. It loads a block that uses url parameter. However, I want my user to be able to create a node with the same url (to override it) and add the specified block in a paragraphs. This way, my client can configure his meta tags and add the content he wants. But the block looses it's context. So I want to reverse engineer this part.
    – JFC
    Commented Feb 9, 2023 at 22:24

1 Answer 1

1

You can use the router.no_access_checks service for this.

$router = \Drupal::service('router.no_access_checks');
$parameters = $router->match($url);

It works on both path aliases as well as unaliased paths. Example for a specific node with id 5 and an alias of article/routing-in-drupal:

$router = \Drupal::service('router.no_access_checks');
$url = 'node/5';
$parameters = $router->match($url);
// $parameters['node']->id() gives 5

$url = 'article/routing-in-drupal';
$parameters = $router->match($url);
// $parameters['node']->id() gives 5
4
  • This is a good answer but it takes the parameters of the path alias and not from a specific route.
    – JFC
    Commented Feb 10, 2023 at 0:11
  • @JFK I don't believe that, and just tested it and can confirm that it gives the route parameters (maybe assuming that they have been setup correctly). Can you maybe add your full route declaration to the question?
    – berliner
    Commented Feb 10, 2023 at 8:26
  • Heres the context. I created a controller page on a specific route. It loads a block that uses the url parameter. However, I want my user to be able to create a node with the same url (to override it) and add the specified block in a paragraphs. This way, my client can configure his meta tags and add the content he wants around the block. But the block looses it's context. In this case, $router->match($url) will return the node parameters and I want the module's route params instead.
    – JFC
    Commented Feb 10, 2023 at 18:38
  • TBH this sounds like a strange approach, without sufficient detail it's a bit difficult to judge, but this might be an XY-problem here.
    – berliner
    Commented Feb 10, 2023 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.