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!
Greg A said this on May 11, 2011 at 12:01 pm |
you can cal get_class() core php function to get class name and get_class_methods(‘class_name’) to get method class name bro..
masaru said this on October 6, 2011 at 10:17 pm |
Thanks!
Martynas said this on January 24, 2012 at 11:13 am |
gr8t work, i find it extremely useful
Sanny said this on May 7, 2013 at 10:46 pm |
Thankyou man! =]
Vinicius said this on May 17, 2013 at 2:16 pm |
tanks for the solution.
anjali said this on October 17, 2013 at 8:24 am |
Thanks buddy 🙂
vee said this on March 24, 2014 at 6:29 am |
thanks a lot….
Wangdu said this on May 8, 2014 at 11:43 am |
Thank buddy. It helped me to work more in CI manners… 🙂
Clicker said this on July 4, 2014 at 8:34 am |