2014-05-22 102 views
4

如何在CodeIgniter中調用一個控制器而不是所有控制器?Codeigniter中的鉤子

例如:我想只運行管理部分的鉤子。我怎樣才能做到這一點?

+0

您可以嘗試在鉤來獲取控制器的名稱,然後只運行代碼當它是'管理員'。 http://ellislab.com/forums/viewthread/62981/#778802 –

回答

4

在您希望有選擇地運行的鉤子中,您可以使用$this->ci =& get_instance();訪問ci超級對象。這充當了一個指針,可以用來訪問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 */ 
1

您可以簡單地通過你的鉤子檢查應用程序的URL做到這一點:

$hook = false; 
if(strpos($_SERVER['REQUEST_URI'],"admin/")) 
$hook = true; 
if($hook) { 
// do some hook stuff 
}