Преглед на файлове

added support for parameters with default null

Lukas Kahwe Smith преди 14 години
родител
ревизия
1ecaade68d
променени са 2 файла, в които са добавени 21 реда и са изтрити 3 реда
  1. 5 2
      src/Symfony/Component/Routing/Generator/UrlGenerator.php
  2. 16 1
      tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

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

@@ -99,8 +99,11 @@ class UrlGenerator implements UrlGeneratorInterface
                         throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $requirements[$token[3]], $tparams[$token[3]]));
                     }
 
-                    // %2F is not valid in a URL, so we don't encode it (which is fine as the requirements explicitely allowed it)
-                    $url = $token[1].str_replace('%2F', '/', urlencode($tparams[$token[3]])).$url;
+                    if (isset($tparams[$token[3]])) {
+                        // %2F is not valid in a URL, so we don't encode it (which is fine as the requirements explicitly allowed it)
+                        $url = $token[1].str_replace('%2F', '/', urlencode($tparams[$token[3]])).$url;
+                    }
+
                     $optional = false;
                 }
             } elseif ('text' === $token[0]) {

+ 16 - 1
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -119,7 +119,22 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals('/app.php/testing/bar', $url);
     }
-    
+
+    public function testRelativeUrlWithNullParameter()
+    {
+        $this->routeCollection->add('test', new Route('/testing.{format}', array('format' => null)));
+        $this->generator->setContext(array(
+            'base_url'=>'/app.php',
+            'method'=>'GET',
+            'host'=>'localhost',
+            'port'=>80,
+            'is_secure'=>false));
+
+        $url = $this->generator->generate('test', array(), false);
+
+        $this->assertEquals('/app.php/testing', $url);
+    }
+
     public function testRelativeUrlWithExtraParameters()
     {
         $this->routeCollection->add('test', new Route('/testing'));