CodeIgniter Forums
Get controller name and method - 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: Get controller name and method (/showthread.php?tid=73765)



Get controller name and method - Smil3y - 06-02-2019

How am I able to get the controller name and method in a filter?


RE: Get controller name and method - InsiteFX - 06-03-2019

I' am not sure if these will work in a filter or not.

get_class ($object); // Returns the name of the class of an object
get_called_class (); // The "Late Static Binding" class name
get_parent_class ($object); // Retrieves the parent class name for object or class
gettype ($var); // Get the type of a variable

is_subclass_of ($object, $class_name, $allow_string = TRUE); // Checks if the object has this class as one of its parents or implements it

get_class_methods ($class_name); // Gets the class methods' names
get_class_vars ($class_name); // Get the default properties of the class
get_object_vars ($object); // Gets the properties of the given object


RE: Get controller name and method - kilishan - 06-03-2019

The routing has already been done at that point since it knows what controller to run, so you can always grab the Router instance and get them from there:

Code:
$router = service('router');
$controller  = $router->controllerName();
$method = $router->methodName();



RE: Get controller name and method - InsiteFX - 06-03-2019

Aww, so we do have something like CI 3 had thanks Lonnie.


RE: Get controller name and method - xGuRux - 06-06-2020

(06-03-2019, 06:34 AM)kilishan Wrote: The routing has already been done at that point since it knows what controller to run, so you can always grab the Router instance and get them from there:

Code:
$router = service('router');
$controller  = $router->controllerName();
$method = $router->methodName();


Hi,

This was exactly what i was looking for, but i want to ask a few things.

By doing $router = service('router'); am i grabbing the already initiated service or creating a new one. 

Also is this a part of the docs, i mean did i miss something, or its something i should have already been aware of.


RE: Get controller name and method - Zeff - 06-04-2021

Thanks guys, Interesting!
I searched the userguide for more info on this router service (and its methods), but I come home empty handed... Suggestions on where to find more?

Cheers,

Zeff