how to pass a variable from one controller to the other in Code igniter

后端 未结 6 930
攒了一身酷
攒了一身酷 2021-02-05 21:25

I have just started learning Code Igniter .

I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?

6条回答
  •  旧时难觅i
    2021-02-05 21:53

    It will depend on the circumstances. If you want to retain the data for some time, then session data would be the way to go. However, if you only need to use it once, flash data might be more appropriate.

    First step would be to initialise the session library:

    $this->load->library('session');
    

    Then store the information in flash data:

    $this->session->set_flashdata('item', $myVar);
    

    Finally, in the second controller, fetch the data:

    $myVar = $this->session->flashdata('item');
    

    Obviously this would mean you'd have to either initialise the session library again from the second controller, or create your own base controller that loads the session library and have both of your controllers inherit from that one.

提交回复
热议问题