applechief
applechief

Reputation: 6895

Codeigniter get controller name in helper

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.

Upvotes: 12

Views: 29682

Answers (4)

Felipe
Felipe

Reputation: 1

You can also use the URI class

$ci = & get_instance();
$ci->uri->segment(1) // That stands for controller
$ci->uri->segment(2) // That stands for method

Upvotes: 0

Waqleh
Waqleh

Reputation: 10161

this should work (not so sure if it works in the helper):

$ci =& get_instance();
$ci->router->class // gets class name (controller)
$ci->router->method // gets function name (controller function)

Upvotes: 0

enapupe
enapupe

Reputation: 17019

$this->>router->fetch_method(); will return index if you do something like this:

class Someclass extends CI_Controller {        
    function index(){        
        $this->edit();        
    }        
    function edit(){        
        $this->router->fetch_method(); //outputs index
    }
}

Upvotes: 0

Ben Swinburne
Ben Swinburne

Reputation: 26467

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.

Upvotes: 24

Related Questions