I have a custom helper that i use for logging.
Within one of the functions of the helper i need to get the name of the controller that was called. Is there a way to do it?
I can't rely on uri segments because some controllers are in sub-folders and the helper is used all over.
Use fetch_class() method to get the name of the controller or class in CodeIgniter. Use fetch_method() method to get the name of the method or function in CodeIgniter.
The pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.
To actually answer your question, $this actually represents the singleton Codeigniter instance (which is actually the controller object). For example when you load libraries/models, you're attaching them to this instance so you can reference them as a property of this instance.
Defining a Default ControllerCodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';
You can use the following in CI2.x
$this->router->fetch_class();
You may need to get an instance of the CI super variable $this first- in which case. Use the following:
$ci =& get_instance();
$ci->router->fetch_class();
There's also a $ci->router->fetch_method();
method if you need the name of the method called for any reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With