CodeIgniter Forums
CodeIgniter v4.0.2: Current class and method names - 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: CodeIgniter v4.0.2: Current class and method names (/thread-75761.html)



CodeIgniter v4.0.2: Current class and method names - Bennito254 - 03-13-2020

How can I fetch the names of the currently displayed controller class and the method?


RE: CodeIgniter v4.0.2: Current class and method names - John_Betong - 03-13-2020

Try this:

https://www.php.net/manual/en/language.constants.predefined.php


RE: CodeIgniter v4.0.2: Current class and method names - Bennito254 - 03-13-2020

What about from a helper, or a library.


RE: CodeIgniter v4.0.2: Current class and method names - Bennito254 - 03-13-2020

Found a solution:
Can be used in helpers too.
PHP Code:
    $router = \Config\Services::router();
    $_method $router->methodName();
    $_controller $router->controllerName(); 



RE: CodeIgniter v4.0.2: Current class and method names - MGatner - 03-13-2020

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()


RE: CodeIgniter v4.0.2: Current class and method names - Bennito254 - 03-13-2020

(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.