Description
Symfony version(s) affected: 4.1.1
Description
When I try to get a route from the RouteCollection with RouteCollection->get($routeName)
, I get null
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
/** @var Symfony\Component\HttpFoundation\Request $request */
/** @var Symfony\Component\Routing\RouteCollection $routeCollection */
// gets the base route name without _locale suffix:
$routeName = $request->attributes->get('_route');
// is then always null for internationalized routes:
$routeObject = $routeCollection->get($routeName);
// gets the internationalized route:
$routeObject = $routeCollection->get($routeName . '.' . $request->attributes->get('_locale');
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 the generate()
method is not recommended.
Or the Request could have a _route_compatible_to_route_collection
attribute to feed into RouteCollection::get()
.
My workaround for that issue looks like this:
// a workaround to find routes that are internationalized, since RouteCollection::get() wants to get the route name with its _locale suffix
if (null === $routeObject = $routeCollection->get($routeName)) {
if (null === $routeObject = $routeCollection->get($routeName . '.' . $request->attributes->get('_locale', 'en'))) {
throw new \RuntimeException('No route ' . $routeName . ' found in RouteCollection');
}
}
Greets,
spackmat