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/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Using variables as the controller name for routes (/showthread.php?tid=79412)

Pages: 1 2


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.


RE: Using variables as the controller name for routes - mdlucas - 07-16-2021

I think is not possible.


RE: Using variables as the controller name for routes - John_Betong - 07-16-2021

@eril46,

https://codeigniter.com/user_guide/incoming/controllers.html

Quote:Important

Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.

Note to Development Team:
BaseController.php does not conform to the above restrictions?


RE: Using variables as the controller name for routes - includebeer - 07-16-2021

(07-16-2021, 04:22 AM)John_Betong Wrote: @eril46,

https://codeigniter.com/user_guide/incoming/controllers.html

Quote:Important

Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.

Note to Development Team:
BaseController.php does not conform to the above restrictions?

I think the user guide is wrong. I always have multiple uppercase letters in my controller, like in your example, and it's working fine.