Skip to content

Commit

Permalink
[Routing] also add matched params for redirect due to trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion authored and nicolas-grekas committed Jul 11, 2017
1 parent 2fa7cc5 commit 4af7c54
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added support for prioritized routing loaders.
* Add matched and default parameters to redirect responses

3.3.0
-----
Expand Down
5 changes: 3 additions & 2 deletions Matcher/Dumper/PhpMatcherDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
}
}

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

// optimize parameters array
if ($matches || $hostMatches) {
Expand Down Expand Up @@ -385,7 +386,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
if ($hasTrailingSlash || $schemes) {
$code .= " return \$ret;\n";
} else {
$code = substr_replace($code, 'return', $retOffset + 12, 6);
$code = substr_replace($code, 'return', $retOffset, 6);
}
$code .= " }\n";

Expand Down
4 changes: 2 additions & 2 deletions Matcher/RedirectableUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function match($pathinfo)
}

try {
parent::match($pathinfo.'/');
$parameters = parent::match($pathinfo.'/');

return $this->redirect($pathinfo.'/', null);
return array_replace($parameters, $this->redirect($pathinfo.'/', isset($parameters['_route']) ? $parameters['_route'] : null));
} catch (ResourceNotFoundException $e2) {
throw $e;
}
Expand Down
8 changes: 1 addition & 7 deletions Matcher/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes)

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

if (self::ROUTE_MATCH === $status[0]) {
$attributes = array_replace($matches, $hostMatches, (array) $status[1]);

return $this->mergeDefaults($attributes, $route->getDefaults());
}

if (self::REQUIREMENT_MISMATCH === $status[0]) {
continue;
}

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

Expand Down
23 changes: 19 additions & 4 deletions Tests/Matcher/RedirectableUrlMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testRedirectWhenNoSlash()
$coll->add('foo', new Route('/foo/'));

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

Expand Down Expand Up @@ -69,7 +69,7 @@ public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches()
$matcher->match('/foo');
}

public function testRedirectWithParams()
public function testSchemeRedirectWithParams()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https')));
Expand All @@ -79,8 +79,23 @@ public function testRedirectWithParams()
->expects($this->once())
->method('redirect')
->with('/foo/baz', 'foo', 'https')
->will($this->returnValue(array('_route' => 'foo')))
->will($this->returnValue(array('redirect' => 'value')))
;
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
}

public function testSlashRedirectWithParams()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{bar}/'));

$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
$matcher
->expects($this->once())
->method('redirect')
->with('/foo/baz/', 'foo', null)
->will($this->returnValue(array('redirect' => 'value')))
;
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz'));
}
}

0 comments on commit 4af7c54

Please sign in to comment.