How can I call a hook for only a few controllers in CodeIgniter


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

radhika Train Asked on April 17, 2015 in Software.
Add Comment
1 Answer(s)

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


sahithi_naidu Train Answered on April 17, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.