-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] RouteCollection::get() doesn't find internationalized Routes #27872
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
Comments
A RouteColletion doesn't know anything about the locale, and I'm not sure it should actually. |
I see, then I should build a cached version of the information, I need to get for those routes (in this case the requirements for the route to generate links automatically, if possible). I have a cached map from the route name to controller class and name for another service, so this could be extended. But besides the performance issue, I think there should be a more consistent handling of localized route names. The route generator handles the localized routes very well using the current locale, so should the whole routing system IMHO. By the way: in most cases, I need the information only for the current route. Is there another way to get route meta information for the current request's route without waking up the RouteCollection? |
@spackmat no, because these requirements are used only at build time, to generate the code for the matcher and generator. |
Okay, thanks then! I'll extend my cache with the route requirements for that purpose. Before the issue can be closed: What about adding another attribute to the Request holding the actual route name |
I'm not sure I understand the use case this would open. The matcher returns |
I thought of a value being consistent with the actual internationalized route names. I found it quite surprising, to say the least, that With "not consistent" I mean that the |
The current behavior is what allows making i18n routes totally seamless: no other parts need to take care of their internal name, as both the generator and the matcher can "talk" together using the common |
You are right, it is indeed a bit abstract. My usecase is described in my first post and also my way to handle the resolve-problem. But to be more concrete: I have a RouteHelper-service that (amongst other things) generates paths while handling some context-aware route-parameters automatically. To do this, it reads the requirements of the given route name and fills some required parameters with information from the Request and other context information. This is very helpful since the templates just have to ask this service and don't have to hassle with that context stuff. At some point, this abstraction needs to get the requirements and other meta information for the current route. As you described, i18n routes are totally seamless for the matcher and the generator. That is right. But in fact they are not seamless for any other interoperation with the routing system. So code utilizing the routing system doesn't need to deal with i18n, no changes required, which is cool and should be the goal. But then you say, code utilizing the routing system via other interfaces like the RouteCollection should be able to deal with i18n routes. So what about the goal to make i18n routes seamless? Does it apply to the whole routing system or just a part of it? If it applies, RouteCollection::get() should accept the common route names, so that the actual routes are fully transparent from the outside view, but you comprehensible stated before RouteCollection::get() shouldn't be aware of any locale-stuff. But then if it doesn't apply, then it would be great to expose the real route names to the Request to make it easier to deal with it. I hope this makes my proposal a bit more clear: Just add a bit of DX-sugar to avoid the funny surprise, when the RouteCollection doesn't contain a route, the Request says it is the current one. ;) |
This relates to #19302 (and linked issues/PRs): |
Thank you for this issue. |
I don't think it is resolved, but I have a more or less solid workaround for it. So it can be closed if I am the only having this problem. |
let's close then |
Symfony version(s) affected: 4.1.1
Description
When I try to get a route from the RouteCollection with
RouteCollection->get($routeName)
, I getnull
for internationalized routes, unless I suffix$routeName
with the current_locale
. When I take a look into my RouteCollection, all my internationalized routes have their suffixes (which is correct). But getting the current route from the request gets me the route name without this locale suffix. So they are incompatible forcing me to do a workaround anytime I want to get a route object from the RouteCollection by its route name.How to reproduce
Possible Solution
Maybe, the
get()
method of the RouteCollection can try to load a route with the given name and the current Request's _locale. This would be consistent with the handling of the generation of internationalized routes, where feeding the full internationalized route name into thegenerate()
method is not recommended.Or the Request could have a
_route_compatible_to_route_collection
attribute to feed intoRouteCollection::get()
.My workaround for that issue looks like this:
Greets,
spackmat
The text was updated successfully, but these errors were encountered: