Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4667b66

Browse files
[Routing] allow no-slash root on imported routes
1 parent b2fafc6 commit 4667b66

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Loader\Configurator;
1313

14+
use Symfony\Component\Routing\Route;
1415
use Symfony\Component\Routing\RouteCollection;
1516

1617
/**
@@ -40,10 +41,18 @@ public function __destruct()
4041
*
4142
* @return $this
4243
*/
43-
final public function prefix($prefix)
44+
final public function prefix($prefix, bool $trailingSlashOnRoot = true)
4445
{
4546
if (!\is_array($prefix)) {
4647
$this->route->addPrefix($prefix);
48+
if (!$trailingSlashOnRoot) {
49+
$rootPath = (new Route(trim($prefix).'/'))->getPath();
50+
foreach ($this->route->all() as $route) {
51+
if ($route->getPath() === $rootPath) {
52+
$route->setPath(rtrim($rootPath, '/'));
53+
}
54+
}
55+
}
4756
} else {
4857
foreach ($prefix as $locale => $localePrefix) {
4958
$prefix[$locale] = trim(trim($localePrefix), '/');
@@ -55,13 +64,13 @@ final public function prefix($prefix)
5564
$localizedRoute = clone $route;
5665
$localizedRoute->setDefault('_locale', $locale);
5766
$localizedRoute->setDefault('_canonical_route', $name);
58-
$localizedRoute->setPath($localePrefix.$route->getPath());
67+
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
5968
$this->route->add($name.'.'.$locale, $localizedRoute);
6069
}
6170
} elseif (!isset($prefix[$locale])) {
6271
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix in its parent collection.', $name, $locale));
6372
} else {
64-
$route->setPath($prefix[$locale].$route->getPath());
73+
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
6574
$this->route->add($name, $route);
6675
}
6776
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
158158
$host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
159159
$schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
160160
$methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
161+
$trailingSlashOnRoot = $node->hasAttribute('trailing-slash-on-root') ? $node->getAttribute('trailing-slash-on-root') : true;
161162

162163
list($defaults, $requirements, $options, $condition, /* $paths */, $prefixes) = $this->parseConfigs($node, $path);
163164

@@ -172,6 +173,14 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
172173

173174
if ('' !== $prefix || !$prefixes) {
174175
$subCollection->addPrefix($prefix);
176+
if (!$trailingSlashOnRoot) {
177+
$rootPath = (new Route(trim($prefix).'/'))->getPath();
178+
foreach ($subCollection->all() as $route) {
179+
if ($route->getPath() === $rootPath) {
180+
$route->setPath(rtrim($rootPath, '/'));
181+
}
182+
}
183+
}
175184
} else {
176185
foreach ($prefixes as $locale => $localePrefix) {
177186
$prefixes[$locale] = trim(trim($localePrefix), '/');
@@ -181,15 +190,15 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $
181190
$subCollection->remove($name);
182191
foreach ($prefixes as $locale => $localePrefix) {
183192
$localizedRoute = clone $route;
184-
$localizedRoute->setPath($localePrefix.$route->getPath());
193+
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
185194
$localizedRoute->setDefault('_locale', $locale);
186195
$localizedRoute->setDefault('_canonical_route', $name);
187196
$subCollection->add($name.'.'.$locale, $localizedRoute);
188197
}
189198
} elseif (!isset($prefixes[$locale])) {
190199
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix when imported in "%s".', $name, $locale, $path));
191200
} else {
192-
$route->setPath($prefixes[$locale].$route->getPath());
201+
$route->setPath($prefixes[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
193202
$subCollection->add($name, $route);
194203
}
195204
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class YamlFileLoader extends FileLoader
2929
{
3030
private static $availableKeys = array(
31-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix',
31+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root',
3232
);
3333
private $yamlParser;
3434

@@ -155,6 +155,7 @@ protected function parseImport(RouteCollection $collection, array $config, $path
155155
$condition = isset($config['condition']) ? $config['condition'] : null;
156156
$schemes = isset($config['schemes']) ? $config['schemes'] : null;
157157
$methods = isset($config['methods']) ? $config['methods'] : null;
158+
$trailingSlashOnRoot = $config['trailing_slash_on_root'] ?? true;
158159

159160
if (isset($config['controller'])) {
160161
$defaults['_controller'] = $config['controller'];
@@ -167,6 +168,14 @@ protected function parseImport(RouteCollection $collection, array $config, $path
167168

168169
if (!\is_array($prefix)) {
169170
$subCollection->addPrefix($prefix);
171+
if (!$trailingSlashOnRoot) {
172+
$rootPath = (new Route(trim($prefix).'/'))->getPath();
173+
foreach ($subCollection->all() as $route) {
174+
if ($route->getPath() === $rootPath) {
175+
$route->setPath(rtrim($rootPath, '/'));
176+
}
177+
}
178+
}
170179
} else {
171180
foreach ($prefix as $locale => $localePrefix) {
172181
$prefix[$locale] = trim(trim($localePrefix), '/');
@@ -178,13 +187,13 @@ protected function parseImport(RouteCollection $collection, array $config, $path
178187
$localizedRoute = clone $route;
179188
$localizedRoute->setDefault('_locale', $locale);
180189
$localizedRoute->setDefault('_canonical_route', $name);
181-
$localizedRoute->setPath($localePrefix.$route->getPath());
190+
$localizedRoute->setPath($localePrefix.(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
182191
$subCollection->add($name.'.'.$locale, $localizedRoute);
183192
}
184193
} elseif (!isset($prefix[$locale])) {
185194
throw new \InvalidArgumentException(sprintf('Route "%s" with locale "%s" is missing a corresponding prefix when imported in "%s".', $name, $locale, $file));
186195
} else {
187-
$route->setPath($prefix[$locale].$route->getPath());
196+
$route->setPath($prefix[$locale].(!$trailingSlashOnRoot && '/' === $route->getPath() ? '' : $route->getPath()));
188197
$subCollection->add($name, $route);
189198
}
190199
}

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<xsd:attribute name="schemes" type="xsd:string" />
6868
<xsd:attribute name="methods" type="xsd:string" />
6969
<xsd:attribute name="controller" type="xsd:string" />
70+
<xsd:attribute name="trailing-slash-on-root" type="xsd:boolean" />
7071
</xsd:complexType>
7172

7273
<xsd:complexType name="default" mixed="true">

0 commit comments

Comments
 (0)