How to change permission for dynamic routes? I am able to change permission callback perfectly for static urls: eg:
Works fine:

if ($route = $collection-&gt;get('entity.taxonomy_vocabulary.collection')) {
  foreach ($vocabularies as $vocabulary) {
    $perms['administer ' . $vocabulary-&gt;id() . ' vocabulary terms'] = array(
      'title' =&gt; t('Administer %name vocabulary terms', array('%name' =&gt; $vocabulary-&gt;label())),
    );
    $route-&gt;setRequirement('_permission', 'administer ' . $vocabulary-&gt;id() . ' vocabulary terms');
  }
}


But same logic does not work for dynamic routes i.e route having a variable say taxonomy machine name.

Does not work:

if ($route = $collection-&gt;get('entity.taxonomy_term.add_form')) {
        foreach ($vocabularies as $vocabulary) {
            if($taxonomy_vocabulary == $vocabulary-&gt;id) {
                $route-&gt;setRequirement('_permission', 'administer ' . $vocabulary-&gt;id() . ' vocabulary terms');
            }
        }
      }


Please suggest how to alter/change permission callback for taxonomy add/view etc routes.
The _permission route setting isn't a callback function like the access callback in Drupal 7; it defines one or more permissions that are checked before granting access to the route. There's nothing dynamic about the setting. Adding one (or more) 'administer taxonomy_id vocabulary terms' permissions will restrict access if the user doesn't have those one or more permissions.

If you need to define a dynamic access check, you'll need to utilize _custom_access and define your own callback function that takes into account a specific permission check based on the context (see Structure of routes for more information).