问题描述
我怎么只为CodeIgniter中的几个控制器而不是所有控制器调用一个钩子?
How can I call a hook for only a few controllers instead of all controllers in CodeIgniter?
例如:我只想为admin部分运行该钩子。我该如何实现?
E.g.: I want to run the hook for only the admin section. How can I achieve this?
推荐答案
在希望有选择地运行的钩子中,可以使用<$访问ci超级对象。 c $ c> $ this-> ci =& get_instance(); 。这充当指针,可用于访问CodeIgniter路由器,以使用 $ class = $ this-> ci-> router-> fetch_class();
。然后,您可以检查 $ class
是否匹配某个值。这会给你:
In the hook which you wish to run selectively, you can access the ci superobject using $this->ci =& get_instance();
. This acts as a pointer which can be used to access the CodeIgniter router to determine the class using $class = $this->ci->router->fetch_class();
. You can then check if $class
matches a certain value. This would give you:
<?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 */
这篇关于钩在Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!