Procházet zdrojové kódy

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

Fabien Potencier před 14 roky
rodič
revize
7e33159723

+ 2 - 1
src/Symfony/Component/Routing/Generator/UrlGenerator.php

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

+ 24 - 0
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -90,6 +90,30 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
         $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
      * @expectedException \InvalidArgumentException
      */
      */