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

Skip to content

Commit 509d01e

Browse files
[Routing] allow no-slash root on imported routes
1 parent a38cbd0 commit 509d01e

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

src/Symfony/Component/Routing/Route.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function unserialize($serialized)
109109
*/
110110
public function getPath()
111111
{
112-
return $this->path;
112+
return '' === $this->path ? '/' : $this->path;
113113
}
114114

115115
/**
@@ -123,14 +123,20 @@ public function getPath()
123123
*/
124124
public function setPath($pattern)
125125
{
126+
$pattern = trim($pattern);
126127
// A pattern must start with a slash and must not have multiple slashes at the beginning because the
127128
// generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
128-
$this->path = '/'.ltrim(trim($pattern), '/');
129+
$this->path = '' !== $pattern && '/' !== $pattern ? '/'.ltrim($pattern, '/') : $pattern;
129130
$this->compiled = null;
130131

131132
return $this;
132133
}
133134

135+
public function hasTrailingSlash(): bool
136+
{
137+
return '' !== $this->path && '/' === $this->path[-1];
138+
}
139+
134140
/**
135141
* Returns the pattern for the host.
136142
*

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
147147
}
148148

149149
foreach ($this->routes as $route) {
150-
$route->setPath('/'.$prefix.$route->getPath());
150+
$route->setPath('/'.$prefix.(!$route->hasTrailingSlash() && '/' === $route->getPath() ? '' : $route->getPath()));
151151
$route->addDefaults($defaults);
152152
$route->addRequirements($requirements);
153153
}

src/Symfony/Component/Routing/Tests/Fixtures/controller/routing.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<route id="app_homepage" path="/" controller="AppBundle:Homepage:show" />
88

9+
<route id="app_homepage_no_slash" path="" controller="AppBundle:Homepage:show" />
10+
911
<route id="app_blog" path="/blog">
1012
<default key="_controller">AppBundle:Blog:list</default>
1113
</route>

src/Symfony/Component/Routing/Tests/Fixtures/controller/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ app_homepage:
22
path: /
33
controller: AppBundle:Homepage:show
44

5+
app_homepage_no_slash:
6+
path: ''
7+
controller: AppBundle:Homepage:show
8+
59
app_blog:
610
path: /blog
711
defaults:

src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ public function testLoadRouteWithControllerAttribute()
296296
$route = $routeCollection->get('app_homepage');
297297

298298
$this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
299+
300+
$route = $routeCollection->get('app_homepage_no_slash');
301+
302+
$this->assertSame('/', $route->getPath());
299303
}
300304

301305
public function testLoadRouteWithoutControllerAttribute()
@@ -371,5 +375,11 @@ public function testImportRouteWithNamePrefix()
371375
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
372376
$this->assertNotNull($routeCollection->get('api_app_blog'));
373377
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
378+
379+
$route = $routeCollection->get('api_app_homepage');
380+
$this->assertSame('/api/', $route->getPath());
381+
382+
$route = $routeCollection->get('api_app_homepage_no_slash');
383+
$this->assertSame('/api', $route->getPath());
374384
}
375385
}

src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public function testLoadRouteWithControllerAttribute()
117117
$route = $routeCollection->get('app_homepage');
118118

119119
$this->assertSame('AppBundle:Homepage:show', $route->getDefault('_controller'));
120+
121+
$route = $routeCollection->get('app_homepage_no_slash');
122+
123+
$this->assertSame('/', $route->getPath());
120124
}
121125

122126
public function testLoadRouteWithoutControllerAttribute()
@@ -192,5 +196,11 @@ public function testImportRouteWithNamePrefix()
192196
$this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath());
193197
$this->assertNotNull($routeCollection->get('api_app_blog'));
194198
$this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath());
199+
200+
$route = $routeCollection->get('api_app_homepage');
201+
$this->assertSame('/api/', $route->getPath());
202+
203+
$route = $routeCollection->get('api_app_homepage_no_slash');
204+
$this->assertSame('/api', $route->getPath());
195205
}
196206
}

0 commit comments

Comments
 (0)