Skip to content

Commit 855206b

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

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
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/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)