bug #18280 [Routing] add query param if value is different from default (Tobion)
This PR was merged into the 2.3 branch.
Discussion
----------
[Routing] add query param if value is different from default
| Q | A
| ------------- | ---
| Branch? | 2.3
| Bug fix? | yes
| New feature? | yes
| BC breaks? | most likely not
| Deprecations? | no
| Tests pass? | no
| Fixed tickets | #10940, #18111, #18035
| License | MIT
| Doc PR | -
Commits
-------
1ef2edf
[Routing] add query param if value is different from default
This commit is contained in:
commit
2528ec48cc
@ -257,7 +257,10 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add a query string if needed
|
// add a query string if needed
|
||||||
$extra = array_diff_key($parameters, $variables, $defaults);
|
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, function ($a, $b) {
|
||||||
|
return $a == $b ? 0 : 1;
|
||||||
|
});
|
||||||
|
|
||||||
if ($extra && $query = http_build_query($extra, '', '&')) {
|
if ($extra && $query = http_build_query($extra, '', '&')) {
|
||||||
$url .= '?'.$query;
|
$url .= '?'.$query;
|
||||||
}
|
}
|
||||||
|
@ -291,10 +291,22 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
public function testQueryParamSameAsDefault()
|
public function testQueryParamSameAsDefault()
|
||||||
{
|
{
|
||||||
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
|
$routes = $this->getRoutes('test', new Route('/test', array('page' => 1)));
|
||||||
|
|
||||||
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
|
$this->assertSame('/app.php/test?page=2', $this->getGenerator($routes)->generate('test', array('page' => 2)));
|
||||||
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => 1)));
|
||||||
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('page' => '1')));
|
||||||
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayQueryParamSameAsDefault()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/test', array('array' => array('foo', 'bar'))));
|
||||||
|
|
||||||
|
$this->assertSame('/app.php/test?array%5B0%5D=bar&array%5B1%5D=foo', $this->getGenerator($routes)->generate('test', array('array' => array('bar', 'foo'))));
|
||||||
|
$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'))));
|
||||||
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array('foo', 'bar'))));
|
||||||
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('array' => array(1 => 'bar', 0 => 'foo'))));
|
||||||
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
|
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user