4

we've wanted to implement a policy to consider some URLs as whitelist, I've provided a config form to admin to enter whitelist Urls, separated by newline, it must support * ( wildcard).

sample urls are

/user/*/edit
/coming-soon

and my implementation

$config = $config_factory->get('mint_coming_soon.softlaunchconfigadmin');

$white_route_list = $config->get('white_path_list');
$white_route_list = $white_route_list ? preg_split("/\\r\\n|\\r|\\n/", $white_route_list) : NULL;
$is_white_route = FALSE;
foreach ($white_route_list as $route) {
  $route_name = $this->pathValidator->getUrlIfValid($route);
  $route_name = $route_name ? $route_name->getRouteName() : NULL;
  drupal_set_message($route_name);
  $current_route_name = \Drupal::routeMatch()->getRouteName();
  if ($current_route_name == $route_name) {
    $is_white_route = TRUE;
    break;
  }
}

$this->pathValidator->getUrlIfValid doesn't work on URLs with the wildcard, my question is how can I get the route name of Url which contains a wildcard?

Notice: wildcard in this question is particularly *.

4
$pattern = '/user/*/edit';
$route_provider = \Drupal::service('router.route_provider');
$found_routes = $route_provider->getRoutesByPattern($pattern);
$route_names = [];
foreach ($found_routes as $route_name => $route_object) {
  $route_names[] = $route_name;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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