I have a custom controller, who take the tid parameter :


The tid is upcast with the taxonomy Term so in my controller method I have:

public function index(TaxonomyTerm $tid)

In this function I can get the taxonomy entity.

But for call my controller, it's not SEO friendly... i have : /myroute/43 for example.

So I won't pass the term name, because you can have spécial caractere and it's not a good pratice in a multilingue site.

So for me the best way is to work with pathauto, generate the path for the taxonomy term so i can have :


Now my question is how pass this to my controller.

I need to call my controller like that:

<a href="myroute/{param_taxo}">

So I am not sure how get the tid of the term with friendly url for SEO.

Any experience about that?

  • Normally path aliases are replaced before routing so that you have a valid internal path which matches a route. You can try this on two levels, either within the module pathauto by altering the alias, see pathauto.api.php or in a custom path processor on your own.
    – 4uk4
    Jan 1, 2020 at 21:09
  • ok thank i ll take a look at pathauto.api.php
    – Kevin
    Jan 1, 2020 at 21:26

1 Answer 1


You must edit your route to be like this, /myroute/{alias}.

Then you can load the term in your controller like this:

public function index($alias) {
  // Find the term by path alias.
  $path = \Drupal::service('path_alias.manager')->getPathByAlias($alias);
  if (preg_match('/taxonomy\/term\/(\d+)/', $path, $matches)) {
    $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($matches[1]);
  if (isset($term) && $term instanceof TermInterface) {
    $term_id = $term->id();

NB: The path_alias.manager service is added in Drupal 8.8.x, if you use an older version you must use the path.alias_manager service instead.

  • Ok, so no dynamic upcast, i can't really do like this for load the terms because pathauto use a clean path, so term with apostrophe will be replace with empty value. So i need to load by pathauto for get the term.
    – Kevin
    Jan 1, 2020 at 20:09
  • Try to look at my updated answer :)
    – user72672
    Jan 1, 2020 at 20:12
  • @Kevin Sorry made a mistake in the regex. I have updated it now.
    – user72672
    Jan 1, 2020 at 20:23
  • Interesting thank for this approch, {alias} parameter can contain / ? i'm not sure about that. but your first approch + your second i think that ll work
    – Kevin
    Jan 1, 2020 at 20:28
  • Have you tried to get a path that contains slashed with getPathByAlias(). I think it should work.
    – user72672
    Jan 1, 2020 at 20:33

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.