From 4c1fdd4edfca4523837e49a390b8a682bab7de50 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Sun, 12 Jun 2016 18:04:21 +0200 Subject: [PATCH] [Routing] also add matched params for redirect due to trailing slash --- .../Routing/RedirectableUrlMatcherTest.php | 2 +- src/Symfony/Component/Routing/CHANGELOG.md | 1 + .../Matcher/Dumper/PhpMatcherDumper.php | 5 ++-- .../Matcher/RedirectableUrlMatcher.php | 4 ++-- .../Component/Routing/Matcher/UrlMatcher.php | 8 +------ .../Matcher/RedirectableUrlMatcherTest.php | 23 +++++++++++++++---- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php index fb5395ea6d..438ca2538d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php @@ -33,7 +33,7 @@ class RedirectableUrlMatcherTest extends TestCase 'scheme' => null, 'httpPort' => $context->getHttpPort(), 'httpsPort' => $context->getHttpsPort(), - '_route' => null, + '_route' => 'foo', ), $matcher->match('/foo') ); diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index 07f8280fe5..b79318e29a 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * Added support for prioritized routing loaders. + * Add matched and default parameters to redirect responses 3.3.0 ----- diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 9a221e6c61..cfe8459f4a 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -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"; diff --git a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php index 900c59fa37..3770a9c24c 100644 --- a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php @@ -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; } diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index f90555643a..df80226cc4 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -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())); } } diff --git a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php index eb9a8492c3..ddd2133e96 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php @@ -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')); } }