Codeigniter中的钩子

san*_*a28 4 hook codeigniter

如何在CodeIgniter中仅为少数控制器而不是所有控制器调用挂钩?

例如:我想只为admin部分运行钩子.我怎样才能做到这一点?

小智 5

在您希望有选择地运行的挂钩中,您可以使用ci superobject访问$this->ci =& get_instance();.这充当指针,可用于访问CodeIgniter路由器以确定使用的类$class = $this->ci->router->fetch_class();.然后,您可以检查是否$class匹配特定值.这会给你:

<?php class Post_controller_constructor {
    var $ci;

    function __construct() {

    }

    function index()
    {
        $this->ci =& get_instance();
        $class = $this->ci->router->fetch_class();
        if($class === 'admin') {
            // Hook procedures
        }
    }
}

/* End of file post_controller_constructor.php */
/* Location: ./application/hooks/post_controller_constructor.php */
Run Code Online (Sandbox Code Playgroud)