Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hooks in Codeigniter

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?

like image 503
sandeepmca28 Avatar asked May 22 '14 11:05

sandeepmca28


1 Answers

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 */
like image 71
Courtney7 Avatar answered Oct 24 '22 00:10

Courtney7