0

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' => '\Drupal\example\Controller\ExampleController::content',
    '_title' => 'Hello'
  ),
  // Route requirements:
  array(
    '_permission'  => 'access content',
  )
);
// Add the route under the name 'example.content'.
$route_collection->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

  • Did you add the necessary entry in the routing.yml file? – kiamlaluno Apr 2 '17 at 15:51
  • @kiamlaluno, thanks, do you mind to advise me the 'necessary entry' in routing.yml? i do route_callbacks: with service and made the correspondent services, pointing to the ExampleRoutes::routes that i show above in my questions - do i missed anything else? – AnD Apr 2 '17 at 16:09
  • That is the necessary entry I was referring to. I should check what the module for image styles does, and see if there is something that module does that your code doesn't. – kiamlaluno Apr 2 '17 at 16:28
  • Why do you need to add new routes dynamically? – Eyal Apr 2 '17 at 17:55
  • @Eyal If the OP is showing the code effectively used, that is not even a dynamic route. If that is the effective code they are using, I would not use it. – kiamlaluno Apr 3 '17 at 8:36
4

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

\Drupal::service('router.builder')->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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.