CodeIgniter Forums
Get controller name and method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Get controller name and method (/thread-73765.html)



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.