Welcome Guest, Not a member yet? Register   Sign In
Using variables as the controller name for routes
#1

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!
Reply
#2

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

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?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#4

Not Possible
Reply
#5

(This post was last modified: 06-18-2021, 02:19 AM by ikesela.)

$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
}
Reply
#6

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

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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#8

I think is not possible.
Reply
#9

(This post was last modified: 07-16-2021, 04:31 AM by John_Betong.)

@eril46,

https://codeigniter.com/user_guide/incom...llers.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?
Reply
#10

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

https://codeigniter.com/user_guide/incom...llers.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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB