CodeIgniter Forums
Using variables as the controller name for routes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forum-11.html)
+--- Thread: Using variables as the controller name for routes (/thread-79412.html)



Using variables as the controller name for routes - eri146 - 06-11-2021

Hello, I have a POST request. For example
/admin/something/(:any)

And I would like to use (:any) as a class variable. For example
$routes->post('/admin/something/(:any)', 'admin/$1::methodname');

Using $1 does not work under any circumstance and it seems that this slash is being trimmed in the Router.php system file.
How can I use it as a variable? Additionally, how could I also use the method as a variable after the double colons? :: For example:
$routes->post('/admin/(:any)/(:any)', 'admin/$1::$2');

Thank you!


RE: Using variables as the controller name for routes - eri146 - 06-17-2021

Hello, I'm still looking for this answer if anyone knows. Tongue


RE: Using variables as the controller name for routes - includebeer - 06-17-2021

I’m pretty sure it’s not possible to do something like that. Do you have that much classes in your app that you really don’t want to type them in?


RE: Using variables as the controller name for routes - seunex - 06-17-2021

Not Possible


RE: Using variables as the controller name for routes - ikesela - 06-18-2021

$1 should use as params only, call method from there ,



route >> /admin/something/(:any) , admincontroller :: something/$1

public function something(string $param)
{
... call method here based on param
}


RE: Using variables as the controller name for routes - eri146 - 06-18-2021

Hello and thanks! It seems the quote button doesn't work here but I'll do this instead...

"I’m pretty sure it’s not possible to do something like that. Do you have that much classes in your app that you really don’t want to type them in? "

What it amounts to is dynamic flexibility without relying on a singular controller to do all the heavy lifting. If a single controller has to have 40 switches and load each and every library file (because for some reason I can't get $something = new library_file to work in CI4) it becomes very taxing in memory. Now if it's possible to load library files as they are needed, that would be different and much less of a burden however being able to do what I was explaining in the OP would give so much more power to codeigniter projects.


RE: Using variables as the controller name for routes - includebeer - 06-19-2021

I don’t understand why you’re talking about a single controller that loads every library at once like it’s the only way to do it!
Create all the controllers you need, load only the library you need, then define your routes accordingly or just enable auto-route.

Maybe you just need to read a little more of the user guide or some tutorials. Because CI4 doesn’t have the limitations you think it has.