Fixed absolute url generation for query strings and hash urls

This commit is contained in:
Alexander Schranz 2017-06-22 11:09:30 +02:00 committed by Fabien Potencier
parent 2731787e8b
commit 89ad27d544
2 changed files with 22 additions and 0 deletions

View File

@ -70,6 +70,13 @@ class HttpFoundationExtension extends \Twig_Extension
$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;
}
@ -80,6 +87,12 @@ class HttpFoundationExtension extends \Twig_Extension
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

@ -41,6 +41,15 @@ class HttpFoundationExtensionTest extends \PHPUnit_Framework_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'),
);
}