dynamic route not updated I have dynamic route: from this example

/**
 * @file
 * Contains \Drupal\example\Routing\ExampleRoutes.
 */

namespace Drupal\example\Routing;

use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Defines dynamic routes.
 */
class ExampleRoutes {

public function routes() {
  $route_collection = new RouteCollection();

  $route = new Route(
    // Path to attach this route to:
    '/example',
    // Route defaults:
    array(
      '_controller' =&gt; '\Drupal\example\Controller\ExampleController::content',
    '_title' =&gt; 'Hello'
  ),
  // Route requirements:
  array(
    '_permission'  =&gt; 'access content',
  )
);
// Add the route under the name 'example.content'.
$route_collection-&gt;add('example.content', $route);
return $route_collection;
}

}


the problem here is: the route is not automatically updated (or accessible)
until i clear cache, then it works.

question is: how to make the new created route immediately works?
*i try to avoid clear cache command (which i don't think this is the right solution)

Thanks
To update the routes you have to call RouteBuilder::rebuild:

\Drupal::service('router.builder')-&gt;rebuild();


This is not what you want to run very often during normal operation of the site. This can take several seconds. It's also not possible to run more than one update process simultaneously. So routes are in general static, even the dynamic ones.