8000 [Routing] Duplicate route names silently ignored · Issue #12561 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Duplicate route names silently ignored #12561

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
fazy opened this issue Nov 24, 2014 · 8 comments
Closed

[Routing] Duplicate route names silently ignored #12561

fazy opened this issue Nov 24, 2014 · 8 comments
Labels

Comments

@fazy
Copy link
fazy commented Nov 24, 2014

It's only possible to have one route with a given name, but it is possible for duplicate route names to go undetected because Symfony will silently ignore the issue. I believe the second one found will overwrite the first, although I guess the loading order is non-deterministic (or determined by factors that can easily change).

Given two bundles both trying to define a login route:

    # src/AppBundle/Resources/config/routing.yml
    login:
        path:      /login
        defaults:  { _controller: AppBundle:Login:index }
    # src/AdminBundle/Resources/config/routing.yml
    login:
        path:      /admin/login
        defaults:  { _controller: AdminBundle:Login:index }

Only one of these routes will exist. This might be an extreme example, because usually the changed behaviour is noticeable. But for applications with many bundles or locale-specific routes, or where it's a simple content page instead of login, then it can be missed.

Usually Symfony is quite strict about configuration problems, helping to ensure they're spotted straight away. Is there a reason for the current behaviour, or can the router throw an exception when trying to overwrite a route?

@mvrhov
Copy link
mvrhov commented Nov 24, 2014

AFAIR the last route wins.
The reason, that there is no notice or even exception is that this enables you to override the routes provided by a 3rd party.

@stof
Copy link
Member
stof commented Nov 24, 2014

I believe the second one found will overwrite the first, although I guess the loading order is non-deterministic (or determined by factors that can easily change).

The order is determined by the order in which you import resources: imported resources are loaded in the place they appear in the file.
The only case where you don't control the factor is when loading annotations for a whole directory. The order will then depend on the order in which you filesystem lists files in the directory, and then on the order in which ReflectionClass lists the methods of the class (which is their order in the file AFAIK, but I don't guarantee it, and I'm not sure PHP does either)

@Tobion
Copy link
Contributor
Tobion commented Nov 24, 2014

I think the only solution would be that devs who want to overwrite routes have to add a flag overwrite: true to the definition. Otherwise it will generate a warning log (BC) / throw an exception (BC break).
IMO it would be more explicit and better experience as overwriting is not that common and conflicting routes seems to be common problem.

@thunderer
Copy link
Contributor

That's why early Symfony2 docs suggested to name routes like VendorNameBundle_RouteName so that they won't collide. If two routes of the same name are in the same file the YAML parser will overwrite former with latter and further code can't do anything with it. Also +1 for warning / exception.

@aferrandini
Copy link
Contributor

@stof, could we add a log if a route is overwrite?
It could help some people who is starting with Symfony and has this errors.

@xabbuh
Copy link
Member
xabbuh commented Nov 29, 2014

@aferrandini At least, this won't work reliably when a route is overwritten in the same YAML file since the YAML parser doesn't trigger warnings on duplicate entries (see #11538).

Nonetheless, it could indeed be an improvement to trigger a warning or something like that when a route will be added to a RouteCollection that already contains a route with the same name.

8000

@aferrandini
Copy link
Contributor

@xabbuh that's what I mean.
When a new route is added to a RouteCollection and a route with the same name exists, it should output an route overwritten warning in the log files.

@jakzal jakzal added the Routing label Dec 12, 2014
@fabpot
Copy link
Member
fabpot commented Dec 24, 2014

Closing as this is how YAML works as explained by @xabbuh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants
0