CodeIgniter – get controller name and method name
Recently, I had situation where I needed to get the name of the current controller.
Initially I was just going to use the URI class. However, I happened upon two useful methods that would provide the name of the current controller and the name of the current method:
To get the name of the controller in CodeIgniter:
$this->router->fetch_class();
To get the name of the method in CodeIgniter:
$this->router->fetch_method();
I couldn’t find either of these in the documentation, but I may have missed it. I hope this helps someone.
Update: In 2.0.2 +, the above won’t work because there is no $this->router
The updated code:
To get the name of the controller in CodeIgniter 2.0.2+:
$router =& load_class('Router', 'core'); $router->fetch_class();
To get the name of the method in CodeIgniter 2.0.2+:
$router =& load_class('Router', 'core'); $router->fetch_method();
Hey, that helped. Thanks a lot!
you can cal get_class() core php function to get class name and get_class_methods(‘class_name’) to get method class name bro..
Thanks!
gr8t work, i find it extremely useful
Thankyou man! =]
tanks for the solution.
Thanks buddy 🙂
thanks a lot….
Thank buddy. It helped me to work more in CI manners… 🙂