Codeigniter在帮助器中获取控制器名称

时间:2011-11-17 14:23:04

标签: codeigniter

我有一个用于记录的自定义助手。

在帮助程序的其中一个函数中,我需要获取被调用的控制器的名称。有办法吗?

我不能依赖uri段,因为有些控制器在子文件夹中并且帮助器遍布全部。

4 个答案:

答案 0 :(得分:22)

您可以在CI2.x中使用以下内容

$this->router->fetch_class();

您可能需要首先获取CI超级变量$的实例 - 在这种情况下。使用以下内容:

$ci =& get_instance();
$ci->router->fetch_class();

如果您因任何原因需要调用方法的名称,还有一个$ci->router->fetch_method();方法。

答案 1 :(得分:0)

如果您执行以下操作,

$this->>router->fetch_method();将返回index

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

答案 2 :(得分:0)

这应该有用(不太确定它是否适用于助手):

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

答案 3 :(得分:0)

您也可以使用URI类

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