Pass argument when overriding controller in RouteSubscriber I am having the issue of overriding the default taxonomy controller /taxonomy/term/{tid}.

I have created my own controller which takes {tid} as a parameter and I want to use it instead, so I overrode the default taxonomy controller in the RouteSubscriber like this:

    &lt;?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;

class RouteSubscriber extends RouteSubscriberBase {

  protected function alterRoutes(RouteCollection $collection) {        
    foreach ($collection-&gt;all() as $route) {          
      if ($route = $collection-&gt;get('entity.taxonomy_term.canonical')) {
        $route-&gt;setDefault('_controller', '\Drupal\my_module\Controller\PagerController::handleDefaultPage');        
      }
    }
  }


  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = array('onAlterRoutes', -250);
    return $events;
  }

}


My sample page controller is this:

&lt;?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class PagerController.
 */
class PagerController extends ControllerBase {

  /**
   * Handlepage.
   *
   * @return string
   *   Return Hello string.
   */
  public function handleDefaultPage($tid) {
    return [
      '#type' =&gt; 'markup',
      '#markup' =&gt; 'term: ' . $tid,
    ];
  }
}


And its routing is this:

my_module.pager_controller_handleDefaultPage:
  path: '/taxonomy/term/{tid}'
  defaults:
    _controller: '\Drupal\my_module\Controller\PagerController::handleDefaultPage'
    _title: 'Taxonomy Pager'
  requirements:
    _permission: 'access content'
    _entity_access: 'taxonomy_term.view'
    taxonomy_term: \d+


But then, when I open the taxonomy page, I get this error which means, that I need to pass the $tid param somehow.

RuntimeException: Controller "Drupal\my_module\Controller\PagerController::handleDefaultPage()" requires that you provide a value for the "$tid" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one. in Symfony\Component\HttpKernel\Controller\ArgumentResolver->getArguments() (line 78 of /var/www/html/vendor/symfony/http-kernel/Controller/ArgumentResolver.php). 

How to resolve it?
Actually, I have found an error in my code.
Everything works normally, when the param name and type are set properly.
So the final code should be:

My sample page controller is this:

&lt;?php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\taxonomy\TermInterface;

/**
 * Class PagerController.
 */
class PagerController extends ControllerBase {

  /**
   * Handlepage.
   *
   * @return string
   *   Return Hello string.
   */
  public function handleDefaultPage(TermInterface $taxonomy_term) {
    return [
      '#type' =&gt; 'markup',
      '#markup' =&gt; 'term: ' . $taxonomy_term-&gt;getName(),
    ];
  }
}


And its routing is this:

my_module.pager_controller_handleDefaultPage:
  path: '/taxonomy/term/{taxonomy_term}'
  defaults:
    _controller: '\Drupal\my_module\Controller\PagerController::handleDefaultPage'
    _title: 'Taxonomy Pager'
  requirements:
    _permission: 'access content'
    _entity_access: 'taxonomy_term.view'
    taxonomy_term: \d+