1
votes

I'm using the Symfony CMF Routing Bundle to create dynamic routes (I'm using one example here):

$route = new Route('/dynamic-url');
$route->setMethods("GET");
$route->setDefault('_controller', 'AppBundle:MyRoute:getResponse');

$routeCollection->add('my-dynamic-route', $route);

The response is loaded from the getResponseAction() function inside the MyRouteController:

/**
 * No annotations here, because I want the url to be dynamic from the database
 */
public function getResponseAction(Request $request) {

    return $this->render('dynamic-page-template.html.twig');

}

When I go to '/dynamic-url', it works.

When in another controller, I want to redirect to this dynamic route, like this:

return $this->redirectToRoute('my-dynamic-route');

But I get this error: "None of the chained routers were able to generate route: Route 'my-dynamic-route' not found"

Also interesting: when I go to '/dynamic-url', the dev bar actually says that the Route name is 'my-dynamic-route'.

Edit

When I load all the routes, I don't see my dynamic route names:

$this->get('router')->getRouteCollection();

I think they should be in this list.

3

3 Answers

1
votes

Since it's a dynamic route, which wasn't saved anywhere (like routing.yml ) it will be only availabe for Request where it has been defined. So at the end of Request your app will immediately "forget" about new Route generated at runtime.

When I load all the routes, I don't see my dynamic route names: $this->get('router')->getRouteCollection(); I think they should be in this list.

Actualy No. It depends on where you call $this->get('router')->getRouteCollection(); Just try to call

dump($this->get('router')->getRouteCollection();)

right before the return statement in your Action where you're adding the my-dynamic-route route. I'm sure you'll see your my-dynamic-route in the list of routes, but if you call it anywhere else - you won't see it.

It's less about symfony rather about stateless nature of web (see Why say that HTTP is a stateless protocol?)

0
votes

I started to think about this and pointed your question to an routing issue on symfony-cmf. You tagged with #symfony-cmf and i think this would be important feature for us.

I also think, when you persist your route with /my-route you should also ask the router for that name (or in case of the CMF with an content object with that a route.)

0
votes

If you use the CmfRoutingBundle dynamic router, you should persist your routes to doctrine. The idea of dynamic here is that they can be created at runtime, e.g. in an admin interface or elsewhere by code. The DynamicRouter loads routes from the database.

If you use PHPCR-ODM, the route name is the repository id, typically something like /cms/routes/my-route and you can generate it with that name. If you have access to the route object loaded from the database, you can also generate from that, rather than the path. If you have to hardcode a path in your application, its an indication that probably a normally configured route would be better.

If you just add a route to the route collection on the fly, you would have to make sure that happens in each request, its only available when you add it. With the CMF ChainRouter you could register your own router that does that, but i would not know of a good use case for that. Either you know the route, then you can configure it in the routing.xml|yml|php config file. Or routes are loaded dynamically, in which point you should use DynamicRouter, potentially with a custom route loader.