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

后端 未结 6 921
攒了一身酷
攒了一身酷 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:58

    Stick to sessions where you can. But there's an alternative (for Codeigniter3) that I do not highly recommend. You can also pass the data through the url. You use the url helper and the url segment method in the receiving controller.

    sending controller method

    redirect("controller2/method/datastring", 'refresh');
    

    receiving controller method

    $this->load->helper('url');
    $data = $this->uri->segment(3);
    

    This should work for the default url structure. For a url: website.com/controller/method/data

    To get controller $this->uri->segment(1) To get method $this->uri->segment(2)

    The limitation of this technique is you can only send strings that are allowed in the url so you cannot use special characters (eg. %@$)

提交回复
热议问题