Skip to content

Commit e39b404

Browse files
Tobionnicolas-grekas
authored andcommitted
[Routing] also add matched params for redirect due to trailing slash
1 parent dc3f7a9 commit e39b404

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testRedirectWhenNoSlash()
3333
'scheme' => null,
3434
'httpPort' => $context->getHttpPort(),
3535
'httpsPort' => $context->getHttpsPort(),
36-
'_route' => null,
36+
'_route' => 'foo',
3737
),
3838
$matcher->match('/foo')
3939
);

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
333333
}
334334
}
335335

336-
$retOffset = strlen($code);
336+
// the offset where the return value is appended below, with indendation
337+
$retOffset = 12 + strlen($code);
337338

338339
// optimize parameters array
339340
if ($matches || $hostMatches) {
@@ -385,7 +386,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
385386
if ($hasTrailingSlash || $schemes) {
386387
$code .= " return \$ret;\n";
387388
} else {
388-
$code = substr_replace($code, 'return', $retOffset + 12, 6);
389+
$code = substr_replace($code, 'return', $retOffset, 6);
389390
}
390391
$code .= " }\n";
391392

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 array_replace($parameters, $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: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
163163

164164
$status = $this->handleRouteRequirements($pathinfo, $name, $route);
165165

166-
if (self::ROUTE_MATCH === $status[0]) {
167-
$attributes = array_replace($matches, $hostMatches, (array) $status[1]);
168-
169-
return $this->mergeDefaults($attributes, $route->getDefaults());
170-
}
171-
172166
if (self::REQUIREMENT_MISMATCH === $status[0]) {
173167
continue;
174168
}
175169

176-
return $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
170+
return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array()));
177171
}
178172
}
179173

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testRedirectWhenNoSlash()
2424
$coll->add('foo', new Route('/foo/'));
2525

2626
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
27-
$matcher->expects($this->once())->method('redirect');
27+
$matcher->expects($this->once())->method('redirect')->will($this->returnValue(array()));
2828
$matcher->match('/foo');
2929
}
3030

@@ -69,7 +69,7 @@ public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches()
6969
$matcher->match('/foo');
7070
}
7171

72-
public function testRedirectWithParams()
72+
public function testSchemeRedirectWithParams()
7373
{
7474
$coll = new RouteCollection();
7575
$coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https')));
@@ -79,8 +79,23 @@ public function testRedirectWithParams()
7979
->expects($this->once())
8080
->method('redirect')
8181
->with('/foo/baz', 'foo', 'https')
82-
->will($this->returnValue(array('_route' => 'foo')))
82+
->will($this->returnValue(array('redirect' => 'value')))
83+
;
84+
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
85+
}
86+
87+
public function testSlashRedirectWithParams()
88+
{
89+
$coll = new RouteCollection();
90+
$coll->add('foo', new Route('/foo/{bar}/'));
91+
92+
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
93+
$matcher
94+
->expects($this->once())
95+
->method('redirect')
96+
->with('/foo/baz/', 'foo', null)
97+
->will($this->returnValue(array('redirect' => 'value')))
8398
;
84-
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
99+
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
85100
}
86101
}

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