CodeIgniter Forums
Is it possible to use dynamic controller name in routing? - 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: Is it possible to use dynamic controller name in routing? (/showthread.php?tid=77460)



Is it possible to use dynamic controller name in routing? - T.O.M. - 09-03-2020

I want to use route localization like said here https://codeigniter.com/user_guide/outgoing/localization.html#in-routes
So I need to add route rule like:
PHP Code:
$routes->get('{locale}/books''App\Books::index'); 

But I want to make this rule for all controllers - not to specify rules for all controllers. So I added the rule:
PHP Code:
$routes->add('{locale}/(:segment)(:any)''$1::$2'); 

I have controller "Login" with method "index()". When I go to "mydomain.com/Login" method "index()" is loaded successfull. But when I go to "mydomain.com/en/Login" (as I expect to use my route) I got 404 Error with message - "Controller or its method is not found: \App\Controllers$1::index". But locale is defined and set correctly.

If I change my route to
PHP Code:
$routes->add('{locale}/(:segment)(:any)''Login::$2'); 

then "mydomain.com/en/Login" is loaded successfull as I want. But in this way I have to set routes for every controller and I want to set one route to work with all controllers.

Is it possible to set route with setting of dynamic controller name?


RE: Is it possible to use dynamic controller name in routing? - Ailothaen - 03-17-2022

Hello,

Just registered to comment on this post because I am having the exact same issue, on a website I am migrating from v3 to v4.

Making routes with dynamic controller names was possible on v3, so this is clearly a regression on the v4.
So if I want to keep my URL architecture and stay on CI, I have to create a route for EVERY page on my website... this is possible but quite dirty in my opinion.