Skip to content

Commit 4e2a404

Browse files
committed
bug symfony#23261 Fixed absolute url generation for query strings and hash urls (alexander-schranz)
This PR was squashed before being merged into the 2.7 branch (closes symfony#23261). Discussion ---------- Fixed absolute url generation for query strings and hash urls | Q | A | ------------- | --- | Branch? | 2.7, ... | Bug fix? | yes | New feature? |no | BC breaks? | yes? absolute_url will change its output but the old was incorrect | Deprecations? |no | Tests pass? | yes? | Fixed tickets | fixes symfony#23260 | License | MIT Fixed absolute url generation for query strings Commits ------- 89ad27d Fixed absolute url generation for query strings and hash urls
2 parents 311e627 + 89ad27d commit 4e2a404

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public function generateAbsoluteUrl($path)
7272
$port = ':'.$this->requestContext->getHttpsPort();
7373
}
7474

75+
if ('#' === $path[0]) {
76+
$queryString = $this->requestContext->getQueryString();
77+
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
78+
} elseif ('?' === $path[0]) {
79+
$path = $this->requestContext->getPathInfo().$path;
80+
}
81+
7582
if ('/' !== $path[0]) {
7683
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
7784
}
@@ -82,6 +89,12 @@ public function generateAbsoluteUrl($path)
8289
return $path;
8390
}
8491

92+
if ('#' === $path[0]) {
93+
$path = $request->getRequestUri().$path;
94+
} elseif ('?' === $path[0]) {
95+
$path = $request->getPathInfo().$path;
96+
}
97+
8598
if (!$path || '/' !== $path[0]) {
8699
$prefix = $request->getPathInfo();
87100
$last = strlen($prefix) - 1;

src/Symfony/Bridge/Twig/Tests/Extension/HttpFoundationExtensionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function getGenerateAbsoluteUrlData()
4242
array('http://example.com/baz', 'http://example.com/baz', '/'),
4343
array('https://example.com/baz', 'https://example.com/baz', '/'),
4444
array('//example.com/baz', '//example.com/baz', '/'),
45+
46+
array('http://localhost/foo/bar?baz', '?baz', '/foo/bar'),
47+
array('http://localhost/foo/bar?baz=1', '?baz=1', '/foo/bar?foo=1'),
48+
array('http://localhost/foo/baz?baz=1', 'baz?baz=1', '/foo/bar?foo=1'),
49+
50+
array('http://localhost/foo/bar#baz', '#baz', '/foo/bar'),
51+
array('http://localhost/foo/bar?0#baz', '#baz', '/foo/bar?0'),
52+
array('http://localhost/foo/bar?baz=1#baz', '?baz=1#baz', '/foo/bar?foo=1'),
53+
array('http://localhost/foo/baz?baz=1#baz', 'baz?baz=1#baz', '/foo/bar?foo=1'),
4554
);
4655
}
4756

0 commit comments

Comments
 (0)