Codeigniter : calling a method of one controller from other

后端 未结 10 1932
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 01:48

I have two controllers a and b.

I would like to call a method of controller a from a method of controller b.

10条回答
  •  既然无缘
    2020-12-05 02:23

    Very simple way in codeigniter to call a method of one controller to other controller
    
    1. Controller A 
       class A extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
        }
        function custom_a()
        {
        }
    }
    
    2. Controller B 
    
       class B extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
        }
        function custom_b()
        {
                require_once(APPPATH.'controllers/a.php'); //include controller
                $aObj = new a();  //create object 
                $aObj->custom_a(); //call function
        }
    }
    

提交回复
热议问题