How do I use PathMatcher::matchPath() for a wildcard path? I am trying to use PathMatcher::matchPath() to match a string of paths using a wildcard pattern. 

I'd like to match any path containing /topic* so if the path matches, I'd like to add a body class for pages that have /topic, /topic/foo, /topic/bar, etc.. 

The code I am using is the following.

function mytheme_preprocess_html(array &amp;$vars) {
  // Define the current path.
  $current_path = \Drupal::service('path.current')-&gt;getPath();
  // Define the pattern using a string.
  $patterns = "/topic/*";
  // If the path matches the pattern.
  if (\Drupal::service('path.matcher')-&gt;matchPath($current_path, $patterns)) {
    $vars['attributes']['class'][] = 'is-topic';
  }
}


The code doesn't work to match this pattern so no class is added when I am on any of these pages. 

My Xdebug info is as follows for the page that has the path topic/review-procedures

$current_path = /node/62
$path_alias = topic/review-procedures


Note that some pages with /topic/ in the path will not be nodes however, that is why I am trying to do the path matching.

Am I correctly using PathMatcher::matchPath()?
In your sample you were comparing the internal path (/node/123) with a path alias pattern (/topic/*). This won't work. You need to do the matching with the path alias instead.

/**
 * Implements template_preprocess_html().
 */
function MYTHEME_preprocess_html(&amp;$variables) {
  $current_path = \Drupal::service('path.current')-&gt;getPath();
  $current_alias = \Drupal::service('path.alias_manager')-&gt;getAliasByPath($current_path);
  if (\Drupal::service('path.matcher')-&gt;matchPath($current_alias, '/topic/*')) {
    $variables['attributes']['class'][] = 'is-topic';
  }
}


You could achieve the same by exploding the current request URI and then see if the first key is topic.

/**
 * Implements template_preprocess_html().
 */
function MYTHEME_preprocess_html(&amp;$variables) {
  $request_uri = ltrim(\Drupal::request()-&gt;getRequestUri(), '/');
  $args = explode('/', $request_uri);
  if (isset($args[0]) &amp;&amp; $args[0] === 'topic') {
    $variables['attributes']['class'][] = 'is-topic';
  }
}