CodeIgniter Forums
How can I get dynamic routes - 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: How can I get dynamic routes (/showthread.php?tid=74922)



How can I get dynamic routes - Michal_PB1 - 11-25-2019

Hi everyone,

How can I get dynamic routes? For example, I have ecommerce system and there I may have posts, manufacturers, and products. All of these paths I have in DB so how can I distinguish that? I want to have only the product name in path without id or any 'product/', 'post/', ''manufacturer/' in path.

I need something like routes based on db.

For example:

URL: /my-lovely-tshirt
Controller: ProductController:Confusedhow(15)

URL: /best-manufacturer

Controller: ManufacturerController:Confusedhow(3)


Thank for answers Cool


RE: How can I get dynamic routes - Michal_PB1 - 11-27-2019

One idea which I have at the moment is: using code as below in /Config/Routes.php file:

PHP Code:
$db = \Config\Database::connect();
$paths $db->table('paths')->get()->getResult();

foreach (
$paths as $path)
{
    $routes->get($path->pathucfirst($path->controller).'Controller::show/'.$path->path);



I know that isn't a perfect solution but only one at the moment. Maybe someone has a better idea for this. I could also generate a file with generated all available paths and include in Routes.php (for better performance than reading all-time)