浏览代码

[Routing] removing the routing hack where we add a / at the beginning if it does not exist

Fabien Potencier 14 年之前
父节点
当前提交
9619c7dade

+ 1 - 1
src/Symfony/Component/Routing/Matcher/UrlMatcher.php

@@ -102,7 +102,7 @@ class UrlMatcher implements UrlMatcherInterface
     {
         // ensure that the URL starts with a /
         if ('/' !== substr($url, 0, 1)) {
-            $url = '/'.$url;
+            throw new \InvalidArgumentException(sprintf('URL "%s" is not valid (it does not start with a /).', $url));
         }
 
         // remove the query string

+ 9 - 2
tests/Symfony/Tests/Component/Routing/Matcher/UrlMatcherTest.php

@@ -24,12 +24,19 @@ class UrlMatcherTest extends \PHPUnit_Framework_TestCase
 
         $matcher = new UrlMatcherForTests($collection, array(), array());
 
-        $this->assertEquals('/', $matcher->normalizeUrl(''), '->normalizeUrl() adds a / at the beginning of the URL if needed');
-        $this->assertEquals('/foo', $matcher->normalizeUrl('foo'), '->normalizeUrl() adds a / at the beginning of the URL if needed');
         $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string');
         $this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /');
     }
 
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testNormalizeUrlThrowsAnExceptionIfTheUrlIsInvalid()
+    {
+        $matcher = new UrlMatcherForTests(new RouteCollection(), array(), array());
+        $matcher->normalizeUrl('');
+    }
+
     public function testMatch()
     {
       // test the patterns are matched are parameters are returned