Score:0

How to get the parameters from a URL and a route that is not the current one?

mx flag
JFK

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();
}

Jaypan avatar
de flag
What is your use case? I can't help but think there is likely a better way to do this
mx flag
JFK
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.
Score:1
bd flag

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
mx flag
JFK
This is a good answer but it takes the parameters of the path alias and not from a specific route.
berliner avatar
bd flag
@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?
mx flag
JFK
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.
berliner avatar
bd flag
TBH this sounds like a strange approach, without sufficient detail it's a bit difficult to judge, but this might be an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.