From 1ecaade68d04655268e44484cea74c50aa7e5ba2 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Mon, 18 Apr 2011 15:35:05 +0200 Subject: [PATCH] added support for parameters with default null --- .../Routing/Generator/UrlGenerator.php | 7 +++++-- .../Routing/Generator/UrlGeneratorTest.php | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index e83d40d61256..79fea790fbcb 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -99,8 +99,11 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa 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]) { diff --git a/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php index 5cdf04393a83..3e0bd24332e2 100644 --- a/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php +++ b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php @@ -119,7 +119,22 @@ public function testRelativeUrlWithParameter() $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'));