Skip to content

Commit ed18eb4

Browse files
committed
Merge pull request symfony#58 from uwej711/unset_locale_if_not_needed
Unset locale if not needed
2 parents 71e3f4c + a7f2de1 commit ed18eb4

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

ContentAwareGenerator.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function generate($name, $parameters = array(), $absolute = false)
6161
throw new RouteNotFoundException('Route of this document is not an instance of Symfony\Component\Routing\Route but: '.$hint);
6262
}
6363

64+
$this->unsetLocaleIfNotNeeded($route, $parameters);
65+
6466
return parent::generate($route, $parameters, $absolute);
6567
}
6668

@@ -74,7 +76,7 @@ public function generate($name, $parameters = array(), $absolute = false)
7476
*
7577
* @throws RouteNotFoundException if there is no route found for the provided name
7678
*/
77-
protected function getRouteByName($name, array &$parameters)
79+
protected function getRouteByName($name, array $parameters)
7880
{
7981
$route = $this->provider->getRouteByName($name, $parameters);
8082
if (empty($route)) {
@@ -93,7 +95,7 @@ protected function getRouteByName($name, array &$parameters)
9395
*
9496
* @return SymfonyRoute either the passed route or an alternative with better locale
9597
*/
96-
protected function getBestLocaleRoute(SymfonyRoute $route, &$parameters)
98+
protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
9799
{
98100
if (! $route instanceof RouteObjectInterface) {
99101
// this route has no content, we can't get the alternatives
@@ -106,7 +108,6 @@ protected function getBestLocaleRoute(SymfonyRoute $route, &$parameters)
106108
$routes = $content->getRoutes();
107109
$contentRoute = $this->getRouteByLocale($routes, $locale);
108110
if ($contentRoute) {
109-
unset($parameters['_locale']);
110111
return $contentRoute;
111112
}
112113
}
@@ -167,7 +168,6 @@ protected function getRouteByContent($name, &$parameters)
167168

168169
$route = $this->getRouteByLocale($routes, $this->getLocale($parameters));
169170
if ($route) {
170-
unset($parameters['_locale']);
171171
return $route;
172172
}
173173

@@ -251,4 +251,23 @@ public function getRouteDebugMessage($name, array $parameters = array())
251251

252252
return parent::getRouteDebugMessage($name, $parameters);
253253
}
254+
255+
/**
256+
* Unset the _locale parameter if it is there and not needed
257+
*
258+
* @param SymfonyRoute $route
259+
* @param array $parameters
260+
*/
261+
protected function unsetLocaleIfNotNeeded(SymfonyRoute $route, array &$parameters)
262+
{
263+
$locale = $this->getLocale($parameters);
264+
if (null !== $locale) {
265+
if (preg_match('/'.$route->getRequirement('_locale').'/', $locale) && $locale == $route->getDefault('_locale')) {
266+
$compiledRoute = $route->compile();
267+
if (!in_array('_locale', $compiledRoute->getVariables())) {
268+
unset($parameters['_locale']);
269+
}
270+
}
271+
}
272+
}
254273
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy