[Routing] also add matched params for redirect due to trailing slash

This commit is contained in:
Tobias Schultze 2016-06-12 18:04:21 +02:00 committed by Nicolas Grekas
parent dc3f7a9ebd
commit 4c1fdd4edf
6 changed files with 27 additions and 16 deletions

View File

@ -33,7 +33,7 @@ class RedirectableUrlMatcherTest extends TestCase
'scheme' => null,
'httpPort' => $context->getHttpPort(),
'httpsPort' => $context->getHttpsPort(),
'_route' => null,
'_route' => 'foo',
),
$matcher->match('/foo')
);

View File

@ -5,6 +5,7 @@ CHANGELOG
-----
* Added support for prioritized routing loaders.
* Add matched and default parameters to redirect responses
3.3.0
-----

View File

@ -333,7 +333,8 @@ EOF;
}
}
$retOffset = strlen($code);
// the offset where the return value is appended below, with indendation
$retOffset = 12 + strlen($code);
// optimize parameters array
if ($matches || $hostMatches) {
@ -385,7 +386,7 @@ EOF;
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";

View File

@ -32,9 +32,9 @@ abstract class RedirectableUrlMatcher extends UrlMatcher implements Redirectable
}
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;
}

View File

@ -163,17 +163,11 @@ class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
$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()));
}
}

View File

@ -24,7 +24,7 @@ class RedirectableUrlMatcherTest extends TestCase
$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');
}
@ -69,7 +69,7 @@ class RedirectableUrlMatcherTest extends TestCase
$matcher->match('/foo');
}
public function testRedirectWithParams()
public function testSchemeRedirectWithParams()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https')));
@ -79,8 +79,23 @@ class RedirectableUrlMatcherTest extends TestCase
->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'), $matcher->match('/foo/baz'));
$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', 'redirect' => 'value'), $matcher->match('/foo/baz'));
}
}