From d2f3092a5b13aabffc30c272eb0178093e43de4c Mon Sep 17 00:00:00 2001 From: Instrye Date: Fri, 10 Jan 2020 15:38:20 +0800 Subject: [PATCH 1/3] fix. route redirect --- system/CodeIgniter.php | 2 +- system/Router/Router.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index e4a9166322a1..7ecf59a7442d 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -252,7 +252,7 @@ public function run(RouteCollectionInterface $routes = null, bool $returnRespons // If the route is a 'redirect' route, it throws // the exception with the $to as the message - $this->response->redirect($e->getMessage(), 'auto', $e->getCode()); + $this->response->redirect(base_url($e->getMessage()), 'auto', $e->getCode()); $this->sendResponse(); $this->callExit(EXIT_SUCCESS); diff --git a/system/Router/Router.php b/system/Router/Router.php index 185e94fb2000..8e43e2553419 100644 --- a/system/Router/Router.php +++ b/system/Router/Router.php @@ -424,6 +424,12 @@ protected function checkRoutes(string $uri): bool // Does the RegEx match? if (preg_match('#^' . $key . '$#', $uri, $matches)) { + + // Is this route supposed to redirect to another? + if ($this->collection->isRedirect($key)) + { + throw new RedirectException(key($val), $this->collection->getRedirectCode($key)); + } // Store our locale so CodeIgniter object can // assign it to the Request. if (isset($localeSegment)) @@ -482,12 +488,6 @@ protected function checkRoutes(string $uri): bool $val = $controller . '::' . $method; } - // Is this route supposed to redirect to another? - if ($this->collection->isRedirect($key)) - { - throw new RedirectException($val, $this->collection->getRedirectCode($key)); - } - $this->setRequest(explode('/', $val)); $this->matchedRoute = [ From c11007f570d45c67f759ff80ae4a32e9355e8111 Mon Sep 17 00:00:00 2001 From: Instrye Date: Fri, 10 Jan 2020 15:49:19 +0800 Subject: [PATCH 2/3] add redirect route in get method --- system/Router/RouteCollection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/Router/RouteCollection.php b/system/Router/RouteCollection.php index c486f5baab20..edec60abba3b 100644 --- a/system/Router/RouteCollection.php +++ b/system/Router/RouteCollection.php @@ -653,6 +653,10 @@ public function addRedirect(string $from, string $to, int $status = 302) { $to = $this->routes['*'][$to]['route']; } + else if(array_key_exists($to, $this->routes['get'])) + { + $to = $this->routes['get'][$to]['route']; + } $this->create('*', $from, $to, ['redirect' => $status]); From 951e2441bc98770b932117603938627a9e28581e Mon Sep 17 00:00:00 2001 From: Instrye Date: Fri, 10 Jan 2020 16:20:05 +0800 Subject: [PATCH 3/3] route redirect get method test --- tests/system/Router/RouteCollectionTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 2a058d0d7a45..753a00fd624b 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -907,6 +907,24 @@ public function testAddRedirectNamed() $this->assertEquals(307, $routes->getRedirectCode('users')); } + public function testAddRedirectGetMethod(){ + $routes = $this->getCollector(); + + $routes->get('zombies', 'Zombies::index', ['as' => 'namedRoute']); + $routes->addRedirect('users', 'namedRoute', 307); + + $expected = [ + 'users' => [ + 'zombies' => '\Zombies::index', + ], + 'zombies' => '\Zombies::index', + ]; + + $this->assertEquals($expected, $routes->getRoutes()); + $this->assertTrue($routes->isRedirect('users')); + $this->assertEquals(307, $routes->getRedirectCode('users')); + } + //-------------------------------------------------------------------- /**