Skip to content

Fixed router doesn't take additional default values #18111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa

// add a query string if needed
$extra = array_diff_key($parameters, $variables, $defaults);
foreach($parameters as $key => $value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space after foreach

if (isset($defaults[$key]) && !array_key_exists($key, $variables) && $defaults[$key] !== $value) {
$extra[$key] = $value;
}
}
if ($extra && $query = http_build_query($extra, '', '&')) {
$url .= '?'.$query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,27 @@ public function testAbsoluteUrlWithExtraParameters()
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
}

public function testAbsoluteUrlWithExtraParametersAndDefault()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is not needed as its the same as the one you changed below

{
$routes = $this->getRoutes('test', new Route('/testing', array('foo' => 'bell')));
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
}

public function testAbsoluteUrlWithExtraParametersAndArrayDefault()
{
$routes = $this->getRoutes('test', new Route('/testing', array('foo' => array('bell'))));
$url = $this->getGenerator($routes)->generate('test', array('foo' => array('bar')), UrlGeneratorInterface::ABSOLUTE_URL);
$this->assertEquals('http://localhost/app.php/testing?foo%5B0%5D=bar', $url);
}

public function testAbsoluteUrlWithoutExtraParametersAndArrayDefault()
{
$routes = $this->getRoutes('test', new Route('/testing', array('foo' => array('bell'))));
$url = $this->getGenerator($routes)->generate('test', array('foo' => array('bell')), UrlGeneratorInterface::ABSOLUTE_URL);
$this->assertEquals('http://localhost/app.php/testing', $url);
}

public function testUrlWithNullExtraParameters()
{
$routes = $this->getRoutes('test', new Route('/testing'));
Expand Down Expand Up @@ -293,7 +314,7 @@ public function testQueryParamSameAsDefault()
{
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));

$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
$this->assertSame('/app.php/test?default=foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
}
Expand Down