8000 Fixed absolute url generation for query strings and hash urls · symfony/symfony@89ad27d · GitHub
[go: up one dir, main page]

Skip to content

Commit 89ad27d

Browse files
alexander-schranzfabpot
authored andcommitted
Fixed absolute url generation for query strings and hash urls
1 parent 2731787 commit 89ad27d

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
@@ -70,6 +70,13 @@ public function generateAbsoluteUrl($path)
7070
$port = ':'.$this->requestContext->getHttpsPort();
7171
}
7272

73+
if ('#' === $path[0]) {
74+
$queryString = $this->requestContext->getQueryString();
75+
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
76+
} elseif ('?' === $path[0]) {
77+
$path = $this->requestContext->getPathInfo().$path;
78+
}
79+
7380
if ('/' !== $path[0]) {
7481
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
7582
}
@@ -80,6 +87,12 @@ public function generateAbsoluteUrl($path)
8087
return $path;
8188
}
8289

90+
if ('#' === $path[0]) {
91+
$path = $request->getRequestUri().$path;
92+
} elseif ('?' === $path[0]) {
93+
$path = $request->getPathInfo().$path;
94+
}
95+
8396
if (!$path || '/' !== $path[0]) {
8497
$prefix = $request->getPathInfo();
8598
$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
@@ -41,6 +41,15 @@ public function getGenerateAbsoluteUrlData()
4141
array('http://example.com/baz', 'http://example.com/baz', '/'),
4242
array('https://example.com/baz', 'https://example.com/baz', '/'),
4343
array('//example.com/baz', '//example.com/baz', '/'),
44+
45+
array('http://localhost/foo/bar?baz', '?baz', '/foo/bar'),
46+
array('http://localhost/foo/bar?baz=1', '?baz=1', '/foo/bar?foo=1'),
47+
array('http://localhost/foo/baz?baz=1', 'baz?baz=1', '/foo/bar?foo=1'),
48+
49+
array('http://localhost/foo/bar#baz', '#baz', '/foo/bar'),
50+
array('http://localhost/foo/bar?0#baz', '#baz', '/foo/bar?0'),
51+
array('http://localhost/foo/bar?baz=1#baz', '?baz=1#baz', '/foo/bar?foo=1'),
52+
array('http://localhost/foo/baz?baz=1#baz', 'baz?baz=1#baz', '/foo/bar?foo=1'),
4453
);
4554
}
4655

0 commit comments

Comments
 (0)
0