How to run alterRoutes on particular condition ? Does it run on cache clear and not on normal Request? I am trying to implement a feature where I need to run different flow than default flow while the registering user on the site.

I am trying to alter the route of "user password reset" for the for the first time logging users, Below is the code

    &lt;?php

    namespace Drupal\developer_registration\Routing;

    use Drupal\Core\Routing\RouteSubscriberBase;
    use Symfony\Component\Routing\RouteCollection;
    use Symfony\Component\HttpFoundation\Request;

    /**
     * Listens to the dynamic route events.
     */
    class RouteSubscriber extends RouteSubscriberBase {

      /**
       * {@inheritdoc}
       */
      protected function alterRoutes(RouteCollection $collection) {
      // The Test is getting printed on only cache clear and not on URL Hit       
      echo "Test"; exit;

      if(isset($_SESSION['developer_registration']['email_id'])) {  

       if ($route = $collection-&gt;get('user.reset')) {
                 $route-&gt;setDefaults(array(
                 '_controller' =&gt; '\Drupal\developer_registration\Controller\UserCustomController::setPasswd',
              ));
             }  
          }
       }
 }


The problem is that above alterRoute method doesn't runs on the URL hit. It only runs on cache. But my problem is that I want to  run alter route on particular condition.

I am not getting the way to do the same. Please help me.
Yes, it runs only on cache clear. You have to implement the dynamic behavior inside of the controller. By extending the core user controller you can call the parent method in case the dynamic condition doesn't apply:

class UserCustomController extends UserController {

  public function resetPass(Request $request, $uid, $timestamp, $hash) {
    if (!...) {
      return parent::resetPass(Request $request, $uid, $timestamp, $hash);
    }

    // return the redirect you want
  }

}