Skip to content

Commit f51da89

Browse files
author
Jérôme Poskin
committed
Prevent unwanted path generation with trailing slashes
1 parent 9021aa8 commit f51da89

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

Routing/I18nAnnotatedRouteControllerLoader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ protected function addRoute(
7070
if($i18n) {
7171
$i18nAnnot->setName($this->helper->alterName($i18nAnnot->getName(), $locale));
7272
$i18nAnnot->setPath($this->helper->alterPath($i18nAnnot->getPath(), $locale));
73-
$i18nAnnot->setDefaults($this->helper->alterdefaults($i18nAnnot->getDefaults(), $locale));
73+
$i18nAnnot->setDefaults($this->helper->alterDefaults($i18nAnnot->getDefaults(), $locale));
7474

7575
if (isset($globals['path']) && !empty($globals['path'])) {
76-
$globals['path'] = $this->helper->alterPath($globals['path'], $locale);
77-
$globals['path'] = rtrim('/' . $locale . '/' . ltrim($globals['path'], '/'), '/');
76+
$globals['path'] = '/' . $locale . '/' . ltrim($this->helper->alterPath($globals['path'], $locale), '/');
77+
} else {
78+
$globals['path'] = '/' . $locale;
7879
}
7980
}
8081

Routing/I18nLoaderHelper.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ public function alterName($name, $locale)
4242
*/
4343
public function alterPath($path, $locale)
4444
{
45-
$translatedPath = $this->translator->trans($path, array(), $this->translationDomain, $locale);
46-
47-
return '/' . trim( $translatedPath, '/');
45+
return $this->translator->trans($path, array(), $this->translationDomain, $locale);
4846
}
4947

5048
/**
5149
* @param array $defaults
5250
* @param string $locale
5351
* @return array
5452
*/
55-
public function alterdefaults(array $defaults, $locale)
53+
public function alterDefaults(array $defaults, $locale)
5654
{
5755
return array_merge($defaults, array('_locale' => $locale));
5856
}

Routing/I18nYamlFileLoader.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class I18nYamlFileLoader extends YamlFileLoader {
2121

2222
/**
2323
* @param FileLocatorInterface $locator
24-
* @param array $locales
25-
* @param string $translationDomain
24+
* @param I18nLoaderHelper $helper
25+
* @param Registry $registry
2626
*/
2727
public function __construct(FileLocatorInterface $locator, I18nLoaderHelper $helper, Registry $registry)
2828
{
@@ -34,14 +34,20 @@ public function __construct(FileLocatorInterface $locator, I18nLoaderHelper $hel
3434

3535
/**
3636
* @param mixed $resource
37-
* @param null $type
37+
* @param string|null $type
3838
* @return bool
3939
*/
4040
public function supports($resource, $type = null)
4141
{
4242
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml_i18n' === $type);
4343
}
4444

45+
/**
46+
* @param RouteCollection $collection
47+
* @param string $name
48+
* @param array $config
49+
* @param string $path
50+
*/
4551
protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
4652
{
4753
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
@@ -56,8 +62,8 @@ protected function parseRoute(RouteCollection $collection, $name, array $config,
5662
$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods);
5763

5864
if($i18n) {
59-
$route->setPath('/' . $locale . $this->helper->alterPath($config['path'], $locale));
60-
$route->setDefaults($this->helper->alterdefaults($defaults, $locale));
65+
$route->setPath('/' . $locale . '/' . ltrim($this->helper->alterPath($config['path'], $locale), '/'));
66+
$route->setDefaults($this->helper->alterDefaults($defaults, $locale));
6167
}
6268

6369
$i18nName = $i18n ? $this->helper->alterName($name, $locale) : $name;

0 commit comments

Comments
 (0)