2011-11-17 56 views
10

我有一个用于日志记录的自定义帮助程序。Codeigniter在帮助程序中获取控制器名称

在辅助函数的其中一个函数中,我需要获取被调用的控制器的名称。有没有办法做到这一点?

我不能依赖uri细分,因为一些控制器在子文件夹中,并且帮助器被全部使用。

回答

22

您可以使用下面的CI2.x

$this->router->fetch_class(); 

您可能需要超级获得CI的实例变量$ this一线在这种情况下。使用以下命令:

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

,如果你需要呼吁任何原因,这种方法的名称,还有一个$ci->router->fetch_method();方法。

+0

这是行得通的。谢谢。 – applechief

0

$this->>router->fetch_method();将返回index,如果你做这样的事情:

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

这应该工作(不是很确定,如果它工作在助手):

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

您也可以使用URI类

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