Skip to content

Commit 4dd2e3e

Browse files
committed
Preserve URI fragment in HttpUtils::generateUri()
1 parent 0256e59 commit 4dd2e3e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class HttpUtils
4141
public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatcher = null)
4242
{
4343
$this->urlGenerator = $urlGenerator;
44-
if ($urlMatcher !== null && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
44+
if (null !== $urlMatcher && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
4545
throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
4646
}
4747
$this->urlMatcher = $urlMatcher;
@@ -150,7 +150,12 @@ public function generateUri($request, $path)
150150
// fortunately, they all are, so we have to remove entire query string
151151
$position = strpos($url, '?');
152152
if (false !== $position) {
153+
$fragment = parse_url($url, PHP_URL_FRAGMENT);
153154
$url = substr($url, 0, $position);
155+
// fragment must be preserved
156+
if ($fragment) {
157+
$url .= "#$fragment";
158+
}
154159
}
155160

156161
return $url;

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ public function testGenerateUriRemovesQueryString()
252252
$this->assertEquals('/foo/bar', $utils->generateUri(new Request(), 'route_name'));
253253
}
254254

255+
public function testGenerateUriPreservesFragment()
256+
{
257+
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value#fragment'));
258+
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
259+
260+
$utils = new HttpUtils($this->getUrlGenerator('/foo/bar#fragment'));
261+
$this->assertEquals('/foo/bar#fragment', $utils->generateUri(new Request(), 'route_name'));
262+
}
263+
255264
/**
256265
* @expectedException \LogicException
257266
* @expectedExceptionMessage You must provide a UrlGeneratorInterface instance to be able to use routes.

0 commit comments

Comments
 (0)