Skip to content

Document to NEVER use getRouteCollection() / how to check if route exists #6710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wouterj opened this issue Jul 2, 2016 · 6 comments
Closed
Labels
actionable Clear and specific issues ready for anyone to take them. good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue. Routing

Comments

@wouterj
Copy link
Member

wouterj commented Jul 2, 2016

It isn't clear for people to not use Router#getRouteCollection(). Using this method will regenerate the Routing cache on each request, making apps very slow. I think we should explicitely tell people to not use this method.

A common use case of this method seems to be checking whether a route exists (symfony/symfony#19270, http://stackoverflow.com/q/14136484/1149495, http://stackoverflow.com/q/17241235/1149495, etc.). Maybe we can document to catch the RouteNotFoundException in such cases and then explain to not use getRouteCollection().

@wouterj wouterj added good first issue Ideal for your first contribution! (some Symfony experience may be required) Doc Request actionable Clear and specific issues ready for anyone to take them. Routing labels Jul 2, 2016
@javiereguiluz
Copy link
Member

@wouterj Symfony provides hasXXX() methods for (almost) everything. Do you know why there isn't a hasRoute() method? Thanks

(In any case, an application which is not sure about its own routes and needs to check if they exist, looks fragile to me).

@wouterj
Copy link
Member Author

wouterj commented Jul 2, 2016

(In any case, an application which is not sure about its own routes and needs to check if they exist, looks fragile to me).

It's less fragile than you might think. Take for example a site using the CMF Routing bundle, which loads routes from a database. An admin might create/remove routes that an app depends on. For example, in the CMF we had to manage the case of a menu item trying to link to a route that was removed.

Symfony provides hasXXX() methods for (almost) everything. Do you know why there isn't a hasRoute() method? Thanks

The router is dumped in cache. This removes the complete $routes array and uses static matching instead (using many nested if statements and such). So in fact, the router doesn't really know about the available routes in production (when it's cached).

The cached generator has a hash map of route name -> route tokens mappings. We might be able to reuse that hash map in the router, but not sure if it's worth it.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 2, 2016

@wouterj: But providing a new Router::hasRoute($name) method in which we can try to generate the route or catch the RouteNotFoundException would help avoiding people to rely on the Router::getRouteCollection() method on runtime. What do you think ?

@TiMESPLiNTER
Copy link

TiMESPLiNTER commented Dec 3, 2016

Maybe we should even consider to set this method to protected? It's only used at one place which is not in the Router class itself (constructor of Symfony\Bundle\WebProfilerBundle\Controller\RouterController). Or at least mark it as @internal.

@mikeploeger
Copy link

mikeploeger commented Aug 24, 2017

My URL structure is like facebook/twitter:
site.com/Username

To let the user set the username, I need to check if a path of a route matches a username.
If getRouteCollection() should not be used, whats the correct way of checking this?

A hasRoute() method like the one in the referenced issue wouldn't help because it only checks by route name and not path.

Update, found the answer:
https://stackoverflow.com/questions/45871463/how-to-check-for-absolute-paths-routes-in-symfony

@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label May 28, 2018
javiereguiluz added a commit that referenced this issue May 29, 2018
This PR was merged into the 2.8 branch.

Discussion
----------

Explain how to check if a route exists

This fixes #6710.

Commits
-------

1c29700 Explain how to check if a route exists
@javiereguiluz
Copy link
Member

Fixed by #9845.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable Clear and specific issues ready for anyone to take them. good first issue Ideal for your first contribution! (some Symfony experience may be required) hasPR A Pull Request has already been submitted for this issue. Routing
Projects
None yet
Development

No branches or pull requests

5 participants