Skip to content

Commit 9a96b6c

Browse files
James Halsallfabpot
James Halsall
authored andcommitted
[Routing] Generate URLs in compliance with PHP_QUERY_RFC3986
1 parent 917c956 commit 9a96b6c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
266266
$fragment = isset($extra['_fragment']) ? $extra['_fragment'] : '';
267267
unset($extra['_fragment']);
268268

269-
if ($extra && $query = http_build_query($extra, '', '&')) {
269+
if ($extra && $query = http_build_query($extra, '', '&', PHP_QUERY_RFC3986)) {
270270
// "/" and "?" can be left decoded for better user experience, see
271271
// http://tools.ietf.org/html/rfc3986#section-3.4
272272
$url .= '?'.strtr($query, array('%2F' => '/'));

Tests/Generator/UrlGeneratorTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,24 @@ public function testGenerateWithSpecialRouteName()
325325

326326
public function testUrlEncoding()
327327
{
328+
if (defined('PHP_QUERY_RFC3986')) {
329+
$expectedPath = '/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
330+
.'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
331+
.'?query=%40%3A%5B%5D/%28%29%2A%27%22%20%2B%2C%3B-._~%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id';
332+
} else {
333+
$expectedPath = '/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
334+
.'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
335+
.'?query=%40%3A%5B%5D/%28%29%2A%27%22+%2B%2C%3B-._%7E%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id';
336+
}
337+
328338
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
329339
// and other special ASCII chars. These chars are tested as static text path, variable path and query param.
330340
$chars = '@:[]/()*\'" +,;-._~&$<>|{}%\\^`!?foo=bar#id';
331341
$routes = $this->getRoutes('test', new Route("/$chars/{varpath}", array(), array('varpath' => '.+')));
332-
$this->assertSame('/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
333-
.'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
334-
.'?query=%40%3A%5B%5D/%28%29%2A%27%22+%2B%2C%3B-._%7E%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id',
335-
$this->getGenerator($routes)->generate('test', array(
336-
'varpath' => $chars,
337-
'query' => $chars,
338-
))
339-
);
342+
$this->assertSame($expectedPath, $this->getGenerator($routes)->generate('test', array(
343+
'varpath' => $chars,
344+
'query' => $chars,
345+
)));
340346
}
341347

342348
public function testEncodingOfRelativePathSegments()

0 commit comments

Comments
 (0)