From ce657f5569bb6a191022b4a7b4896a67cc9dc706 Mon Sep 17 00:00:00 2001 From: MGatner Date: Sat, 20 Apr 2019 22:07:15 -0400 Subject: [PATCH 1/2] Prevent reverseRoute from searching closures Fix for https://github.com/codeigniter4/CodeIgniter4/issues/1953 --- system/Router/RouteCollection.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index 12201a9b428e..1e40215ffdd5 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -1111,6 +1111,12 @@ public function reverseRoute(string $search, ...$params) { $from = key($route['route']); $to = $route['route'][$from]; + + // ignore closures + if (! is_string($to)) + { + continue; + } // Lose any namespace slash at beginning of strings // to ensure more consistent match. From 92ee0ca3da7ff28c38fc5f1a4a0dc79d70c1a7e7 Mon Sep 17 00:00:00 2001 From: MGatner Date: Tue, 14 May 2019 11:44:21 -0400 Subject: [PATCH 2/2] Add test for unmatched reverse route with closure --- tests/system/Router/RouteCollectionTest.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 4871a59b5265..f8ea52988ef2 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -947,15 +947,21 @@ public function testReverseRoutingWithClosure() { $routes = $this->getCollector(); - $routes->add( - 'login', function () { - } - ); + $routes->add('login', function() { }); $match = $routes->reverseRoute('login'); $this->assertEquals('/login', $match); } + + public function testReverseRoutingWithClosureNoMatch() + { + $routes = $this->getCollector(); + + $routes->add('login', function() { }); + + $this->assertFalse($routes->reverseRoute('foobar')); + } //--------------------------------------------------------------------