Welcome Guest, Not a member yet? Register   Sign In
Routes 4.0.0-beta.2
#1

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 ?

Attached Files Thumbnail(s)
   
Reply
#2

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.
Reply
#3

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.
Reply
#4

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 ...
Codeigniter 4 - Docker Image [github] [docker hub]
Reply
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB