Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31e4c6e

Browse files
[Routing] fix trailing slash redirection when using RedirectableUrlMatcher
1 parent303cae1 commit31e4c6e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

‎src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function match($pathinfo)
3232
}
3333

3434
try {
35-
parent::match($pathinfo.'/');
35+
$parameters =parent::match($pathinfo.'/');
3636

37-
return$this->redirect($pathinfo.'/',null);
37+
return$this->redirect($pathinfo.'/',isset($parameters['_route']) ?$parameters['_route'] :null);
3838
}catch (ResourceNotFoundException$e2) {
3939
throw$e;
4040
}

‎src/Symfony/Component/Routing/Matcher/UrlMatcher.php‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,39 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
112112
*/
113113
protectedfunctionmatchCollection($pathinfo,RouteCollection$routes)
114114
{
115+
$supportsTrailingSlash ='/' !==$pathinfo &&'' !==$pathinfo &&$thisinstanceof RedirectableUrlMatcherInterface;
116+
115117
foreach ($routesas$name =>$route) {
116118
$compiledRoute =$route->compile();
119+
$staticPrefix =$compiledRoute->getStaticPrefix();
117120

118121
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
119-
if ('' !==$compiledRoute->getStaticPrefix() &&0 !==strpos($pathinfo,$compiledRoute->getStaticPrefix())) {
122+
if ('' ===$staticPrefix ||0 ===strpos($pathinfo,$staticPrefix)) {
123+
// no-op
124+
}elseif (!$supportsTrailingSlash) {
125+
continue;
126+
}elseif ('/' ===substr($staticPrefix, -1) &&substr($staticPrefix,0, -1) ===$pathinfo) {
127+
return;
128+
}else {
120129
continue;
121130
}
131+
$regex =$compiledRoute->getRegex();
132+
133+
if ($supportsTrailingSlash &&$pos =strpos($regex,'/$')) {
134+
$regex =substr($regex,0,$pos).'/?$'.substr($regex,$pos +2);
135+
$hasTrailingSlash =true;
136+
}else {
137+
$hasTrailingSlash =false;
138+
}
122139

123-
if (!preg_match($compiledRoute->getRegex(),$pathinfo,$matches)) {
140+
if (!preg_match($regex,$pathinfo,$matches)) {
124141
continue;
125142
}
126143

144+
if ($hasTrailingSlash &&'/' !==substr($pathinfo, -1)) {
145+
return;
146+
}
147+
127148
$hostMatches =array();
128149
if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(),$this->context->getHost(),$hostMatches)) {
129150
continue;

‎src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ public function testSchemeRequirement()
8787
$this->assertSame(array('_route' =>'foo'),$matcher->match('/foo'));
8888
}
8989

90+
publicfunctiontestFallbackPage()
91+
{
92+
$coll =newRouteCollection();
93+
$coll->add('foo',newRoute('/foo/'));
94+
$coll->add('bar',newRoute('/{name}'));
95+
96+
$matcher =$this->getUrlMatcher($coll);
97+
$matcher->expects($this->once())->method('redirect')->with('/foo/','foo')->will($this->returnValue(array('_route' =>'foo')));
98+
$this->assertSame(array('_route' =>'foo'),$matcher->match('/foo'));
99+
}
100+
90101
protectedfunctiongetUrlMatcher(RouteCollection$routes,RequestContext$context =null)
91102
{
92103
return$this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher',array($routes,$context ?:newRequestContext()));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp