Skip to content

Commit 6ebfd74

Browse files
committed
Fix routes not matching without trailing /
Routes connected in scopes should match both the / and non / terminated URLs. Refs #4218
1 parent 084651d commit 6ebfd74

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Routing/RouteBuilder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ protected function _makeRoute($route, $defaults, $options) {
346346
unset($options['routeClass']);
347347

348348
$route = str_replace('//', '/', $this->_path . $route);
349+
$route = $route === '/' ? $route : rtrim($route, '/');
350+
349351
foreach ($this->_params as $param => $val) {
350352
if (isset($defaults[$param]) && $defaults[$param] !== $val) {
351353
$msg = 'You cannot define routes that conflict with the scope. ' .

tests/TestCase/Routing/RouteBuilderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ public function testConnectBasic() {
112112
$this->assertEquals($expected, $route->defaults);
113113
}
114114

115+
/**
116+
* Test that compiling a route results in an trailing / optional pattern.
117+
*
118+
* @return void
119+
*/
120+
public function testConnectTrimTrailingSlash() {
121+
$routes = new RouteBuilder($this->collection, '/articles', ['controller' => 'Articles']);
122+
$routes->connect('/', ['action' => 'index']);
123+
124+
$expected = ['plugin' => null, 'controller' => 'Articles', 'action' => 'index', 'pass' => []];
125+
$this->assertEquals($expected, $this->collection->parse('/articles'));
126+
$this->assertEquals($expected, $this->collection->parse('/articles/'));
127+
}
128+
115129
/**
116130
* Test extensions being connected to routes.
117131
*

0 commit comments

Comments
 (0)