Browse Source

Fix routes not matching without trailing /

Routes connected in scopes should match both the / and non / terminated
URLs.

Refs #4218
mark_story 11 years ago
parent
commit
6ebfd743c3
2 changed files with 16 additions and 0 deletions
  1. 2 0
      src/Routing/RouteBuilder.php
  2. 14 0
      tests/TestCase/Routing/RouteBuilderTest.php

+ 2 - 0
src/Routing/RouteBuilder.php

@@ -346,6 +346,8 @@ class RouteBuilder {
 			unset($options['routeClass']);
 
 			$route = str_replace('//', '/', $this->_path . $route);
+			$route = $route === '/' ? $route : rtrim($route, '/');
+
 			foreach ($this->_params as $param => $val) {
 				if (isset($defaults[$param]) && $defaults[$param] !== $val) {
 					$msg = 'You cannot define routes that conflict with the scope. ' .

+ 14 - 0
tests/TestCase/Routing/RouteBuilderTest.php

@@ -113,6 +113,20 @@ class RouteBuilderTest extends TestCase {
 	}
 
 /**
+ * Test that compiling a route results in an trailing / optional pattern.
+ *
+ * @return void
+ */
+	public function testConnectTrimTrailingSlash() {
+		$routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
+		$routes->connect('/', ['action' => 'index']);
+
+		$expected = ['plugin' => null, 'controller' => 'Articles', 'action' => 'index', 'pass' => []];
+		$this->assertEquals($expected, $this->collection->parse('/articles'));
+		$this->assertEquals($expected, $this->collection->parse('/articles/'));
+	}
+
+/**
  * Test extensions being connected to routes.
  *
  * @return void