Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions system/Router/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
12 changes: 6 additions & 6 deletions system/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 = [
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Router/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

//--------------------------------------------------------------------

/**
Expand Down