CodeIgniter Forums

Full Version: CodeIgniter v4.0.2: Current class and method names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I fetch the names of the currently displayed controller class and the method?
What about from a helper, or a library.
Found a solution:
Can be used in helpers too.
PHP Code:
    $router = \Config\Services::router();
    $_method $router->methodName();
    $_controller $router->controllerName(); 
You want the Router class, and the following methods: contrrollerName(), methodName()
You can get it directly from the service as well:

$controller = services(‘router’)->controllerName()
(03-13-2020, 10:54 AM)MGatner Wrote: [ -> ]You want the Router class, and the following methods: contrrollerName(), methodName()
You can get it directly from the service as well:

$controller = services(‘router’)->controllerName()

Yeah, I got that. Admin has not yet approved my comment.