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

~ by ityndall on March 2, 2011.

9 Responses to “CodeIgniter – get controller name and method name”

  1. Hey, that helped. Thanks a lot!

  2. you can cal get_class() core php function to get class name and get_class_methods(‘class_name’) to get method class name bro..

  3. Thanks!

  4. gr8t work, i find it extremely useful

  5. Thankyou man! =]

  6. tanks for the solution.

  7. Thanks buddy 🙂

  8. thanks a lot….

  9. Thank buddy. It helped me to work more in CI manners… 🙂

Leave a Reply