CodeIgniter Forums
Routes 4.0.0-beta.2 - 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: Routes 4.0.0-beta.2 (/showthread.php?tid=73499)



Routes 4.0.0-beta.2 - tony.a - 04-30-2019

Hy all .
I have some user defined routes stored in database to do some redirects .
example user/register (database stored) redirect  to register (app defined route )
My thoughts  of the day were to inject into Config\Routes or Events::on('pre_system'
PHP Code:
$routes->setAutoRoute(false);
$routes->add('login''Login::index');
$routes->add('register''Register::index');

 $routes->addRedirect('user/fff''/register'); //ErrorException Undefined offset: 1 SYSTEMPATH/Router\Router.php at line 472
$routes->addRedirect('user/fff''register' );  //ErrorException strpos() expects parameter 1 to be string, array given
$routes->addRedirect('user/fff''Register::index'); // not working SYSTEMPATH/Router\Router.php : 483 
  
throw RedirectException::forUnableToRedirect($val, $this->collection->getRedirectCode($key)); 

but seems that Config\Routes is called twice  and throw error when adding the redirect
How to inject new routes redirection and where ? Before declaring   route ? after ?


RE: Routes 4.0.0-beta.2 - MGatner - 05-01-2019

There’s probably a better way to do it, but off the top of my head you could create as your last rule a catch-all that maps to a closure that checks your database for a match.


RE: Routes 4.0.0-beta.2 - kilishan - 05-01-2019

Personally - I'd create a controller who's only job was to check the database and handle those redirects. Put a catch-all route as the last one in the file that points to that controller. Or use that controller as your 404Override controller, where you could check those routes first, and redirect if found, or show your custom error page.


RE: Routes 4.0.0-beta.2 - atsanna - 05-01-2019

I solved it this way:
1) I created a table where I store the routes.
2) I created the "RouteModel" model
3) I created the Config\[environment]\Routes.php file with this content

Code:
<?php namespace Config;

use App\Models\RouteModel;
$model = new RouteModel();

$db = Database::connect();
$result = $model->getAll($db);
$db->close();
if( empty($result->getResult()) == false) {
   foreach ($result->getResult() as $row) {
       $routes->add($row->from, $row->to);
   }
}

It will not be the optimal and clean solution, but it has solved my problem.
I hope it can help you in some way ...


RE: Routes 4.0.0-beta.2 - tony.a - 05-04-2019

but seems that Config\Routes is loaded twice .. see pictures