CodeIgniter Forums
Setting redirection in Config/Routes.php seems to be broken - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Setting redirection in Config/Routes.php seems to be broken (/showthread.php?tid=76227)



Setting redirection in Config/Routes.php seems to be broken - HardyW - 04-23-2020

Suppose I have these route definitions in Config/Routes.php:

PHP Code:
$routes->setAutoRoute(true);

/**
* --------------------------------------------------------------------
* Route Definitions
* --------------------------------------------------------------------
*/

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Home::index');

$routes->addRedirect('OldStuff/PHP/API/Synchronization.php',        'Apps/NewStuff/synchronize'); 

The old stuff does not exist anymore (none of it) but is coming from an old implementation of the website. Now, if somebody is entering in the browser

Code:
http://www.mysite.com/OldStuff/PHP/API/Synchronization.php

this leads to a PHP error when throwing an exception in CodeIgniter. So, two errors at once!

Reason:

Router::checkRoutes is checking if the uri (OldStuff/PHP/API/Synchronization.php) can be found in the existing routes. This is obviously the case because the regular expression

PHP Code:
preg_match('#^' $key '$#'$uri$matches
 is true. And this is also the purpose.
If this is true the next statement checks if it is a redirection. Also this is true. Then, a RedirectException is thrown. Why is the exception thrown at all?
Unfortunately, the RedirectException is also called with the wrong parameters and therefore a PHP error occurs.

PHP Code:
throw new RedirectException(key($val), $this->collection->getRedirectCode($key)); 
$val is a simple value and not an array!  Sad

Anyway, the problem is: how do I do redirections in Config/Routes.php if not as shown? Huh