Hooks in Codeigniter

核能气质少年 提交于 2019-12-31 00:23:05

问题


How can I call a hook for only a few controllers instead of all controllers in CodeIgniter?

E.g.: I want to run the hook for only the admin section. How can I achieve this?


回答1:


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 */



回答2:


You can simply do it by checking url of your application in your hook:

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


来源:https://stackoverflow.com/questions/23805693/hooks-in-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!