[Routing] the global parameters must not be added in the QS when generating URLs

This commit is contained in:
Fabien Potencier 2011-04-21 09:52:35 +02:00
parent 286c45733e
commit 7e33159723
2 changed files with 26 additions and 1 deletions

View File

@ -79,6 +79,7 @@ class UrlGenerator implements UrlGeneratorInterface
*/
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
{
$originParameters = $parameters;
$parameters = array_replace($this->context->getParameters(), $parameters);
$tparams = array_replace($defaults, $parameters);
@ -121,7 +122,7 @@ class UrlGenerator implements UrlGeneratorInterface
}
// add a query string if needed
if ($extra = array_diff_key($parameters, $variables, $defaults)) {
if ($extra = array_diff_key($originParameters, $variables, $defaults)) {
$url .= '?'.http_build_query($extra);
}

View File

@ -90,6 +90,30 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
}
public function testUrlWithExtraParametersFromGlobals()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('bar', 'bar');
$generator->setContext($context);
$url = $generator->generate('test', array('foo' => 'bar'));
$this->assertEquals('/app.php/testing?foo=bar', $url);
}
public function testUrlWithGlobalParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('foo', 'bar');
$generator->setContext($context);
$url = $generator->generate('test', array());
$this->assertEquals('/app.php/testing/bar', $url);
}
/**
* @expectedException \InvalidArgumentException
*/