bug #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 #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 #23260
| License       | MIT

Fixed absolute url generation for query strings

Commits
-------

89ad27d544 Fixed absolute url generation for query strings and hash urls
This commit is contained in:
Fabien Potencier 2017-07-06 09:01:21 +03:00
commit 4e2a404f03
2 changed files with 22 additions and 0 deletions

View File

@ -72,6 +72,13 @@ class HttpFoundationExtension extends AbstractExtension
$port = ':'.$this->requestContext->getHttpsPort();
}
if ('#' === $path[0]) {
$queryString = $this->requestContext->getQueryString();
$path = $this->requestContext->getPathInfo().($queryString ? '?'.$queryString : '').$path;
} elseif ('?' === $path[0]) {
$path = $this->requestContext->getPathInfo().$path;
}
if ('/' !== $path[0]) {
$path = rtrim($this->requestContext->getBaseUrl(), '/').'/'.$path;
}
@ -82,6 +89,12 @@ class HttpFoundationExtension extends AbstractExtension
return $path;
}
if ('#' === $path[0]) {
$path = $request->getRequestUri().$path;
} elseif ('?' === $path[0]) {
$path = $request->getPathInfo().$path;
}
if (!$path || '/' !== $path[0]) {
$prefix = $request->getPathInfo();
$last = strlen($prefix) - 1;

View File

@ -42,6 +42,15 @@ class HttpFoundationExtensionTest extends TestCase
array('http://example.com/baz', 'http://example.com/baz', '/'),
array('https://example.com/baz', 'https://example.com/baz', '/'),
array('//example.com/baz', '//example.com/baz', '/'),
array('http://localhost/foo/bar?baz', '?baz', '/foo/bar'),
array('http://localhost/foo/bar?baz=1', '?baz=1', '/foo/bar?foo=1'),
array('http://localhost/foo/baz?baz=1', 'baz?baz=1', '/foo/bar?foo=1'),
array('http://localhost/foo/bar#baz', '#baz', '/foo/bar'),
array('http://localhost/foo/bar?0#baz', '#baz', '/foo/bar?0'),
array('http://localhost/foo/bar?baz=1#baz', '?baz=1#baz', '/foo/bar?foo=1'),
array('http://localhost/foo/baz?baz=1#baz', 'baz?baz=1#baz', '/foo/bar?foo=1'),
);
}