Skip to content

Commit 1ef2edf

Browse files
committed
[Routing] add query param if value is different from default
1 parent a5965fb commit 1ef2edf

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,10 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
257257
}
258258

259259
// add a query string if needed
260-
$extra = array_diff_key($parameters, $variables, $defaults);
260+
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, function ($a, $b) {
261+
return $a == $b ? 0 : 1;
262+
});
263+
261264
if ($extra && $query = http_build_query($extra, '', '&')) {
262265
$url .= '?'.$query;
263266
}

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,22 @@ public function testNullForOptionalParameterIsIgnored()
291291

292292
public function testQueryParamSameAsDefault()
293293
{
294-
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
294+
$routes = $this->getRoutes('test', new Route('/test', array('page' => 1)));
295295

296-
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
297-
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
296+
$this->assertSame('/app.php/test?page=2', $this->getGenerator($routes)->generate('test', array('page' => 2)));
297+
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => 1)));
298+
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => '1')));
299+
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
300+
}
301+
302+
public function testArrayQueryParamSameAsDefault()
303+
{
304+
$routes = $this->getRoutes('test', new Route('/test', array('array' => array('foo', 'bar'))));
305+
306+
$this->assertSame('/app.php/test?array%5B0%5D=bar&array%5B1%5D=foo', $this->getGenerator($routes)->generate('test', array('array' => array('bar', 'foo'))));
307+
$this->assertSame('/app.php/test?array%5Ba%5D=foo&array%5Bb%5D=bar', $this->getGenerator($routes)->generate('test', array('array' => array('a' => 'foo', 'b' => 'bar'))));
308+
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array('foo', 'bar'))));
309+
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array(1 => 'bar', 0 => 'foo'))));
298310
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
299311
}
300312

0 commit comments

Comments
 (0)