Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter get controller name in helper

Tags:

codeigniter

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.

like image 616
applechief Avatar asked Nov 17 '11 14:11

applechief


People also ask

How do I find my CI controller name?

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.

Where is CI_Controller 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.

What is $this in CodeIgniter?

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.

What is the default controller in CodeIgniter?

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 ';


1 Answers

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.

like image 191
Ben Swinburne Avatar answered Oct 17 '22 08:10

Ben Swinburne