diff --git a/src/Symfony/Component/Security/Http/HttpUtils.php b/src/Symfony/Component/Security/Http/HttpUtils.php index 56add79926d3e..01f56b356adaf 100644 --- a/src/Symfony/Component/Security/Http/HttpUtils.php +++ b/src/Symfony/Component/Security/Http/HttpUtils.php @@ -41,7 +41,7 @@ class HttpUtils public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatcher = null) { $this->urlGenerator = $urlGenerator; - if ($urlMatcher !== null && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) { + if (null !== $urlMatcher && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) { throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.'); } $this->urlMatcher = $urlMatcher; @@ -150,7 +150,12 @@ public function generateUri($request, $path) // fortunately, they all are, so we have to remove entire query string $position = strpos($url, '?'); if (false !== $position) { + $fragment = parse_url($url, PHP_URL_FRAGMENT); $url = substr($url, 0, $position); + // fragment must be preserved + if ($fragment) { + $url .= "#$fragment"; + } } return $url; diff --git a/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php b/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php index b508012665e87..457588c180529 100644 --- a/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php +++ b/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php @@ -252,6 +252,15 @@ public function testGenerateUriRemovesQueryString() $this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name')); } + public function testGenerateUriPreservesFragment() + { + $utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value#fragment')); + $this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name')); + + $utils = new HttpUtils($this->getUrlGenerator('/foo/bar#fragment')); + $this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name')); + } + /** * @expectedException \LogicException * @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.