Codeigniter : calling a method of one controller from other

后端 未结 10 1928
爱一瞬间的悲伤
爱一瞬间的悲伤 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:21

    I agree that the way to do is to redirect to the new controller in usual cases.

    I came across a use case where I needed to display the same page to 2 different kind of users (backend user previewing the page of a frontend user) so in my opinion what I needed was genuinely to call the frontend controller from the backend controller.

    I solved the problem by making the frontend method static and wrapping it in another method. Hope it helps!

    //==========
    // Frontend
    //==========
    function profile()
    {
       //Access check
    
       //Get profile id
       $id = get_user_id();
    
       return self::_profile($id);
    }
    
    static function _profile($id)
    {
       $CI = &get_instance();
       //Prepare page
       //Load view
    }
    
    //==========
    // Backend
    //==========
    function preview_profile($id)
    {
       $this->load->file('controllers/frontend.php', false);
    
       Frontend::_profile($id);
    }
    
    0 讨论(0)
  • 2020-12-05 02:22

    You can do like

    $result= file_get_contents(site_url('[ADDRESS TO CONTROLLER FUNCTION]'));
    

    Replace [ADDRESS TO CONTROLLER FUNCTION] by the way we use in site_url();

    You need to echo output in controller function instead of return.

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 2020-12-05 02:27

    You can use the redirect() function. Like this

    class ControllerA extends CI_Controller{
        public function MethodA(){
           redirect("ControllerB/MethodB");
        }
    }
    
    0 讨论(0)
  • 2020-12-05 02:30

    very simple in first controllr call

     $this->load->model('MyController');
     $this->MyController->test();
    

    place file MyController.php to /model patch

    MyController.php should be contain

    class MyController extends CI_Model {
    
        function __construct() {
            parent::__construct();
        }
        function test()
        {
            echo 'OK';
        }
    }
    
    0 讨论(0)
  • 2020-12-05 02:32

    test.php Controller File :

    Class Test {
     function demo() {
      echo "Hello";
     }
    }
    

    test1.php Controller File :

    Class Test1 {
     function demo2() {
      require('test.php');
      $test = new Test();
      $test->demo();
     }
    }
    
    0 讨论(0)
提交回复
热议问题