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

后端 未结 6 925
攒了一身酷
攒了一身酷 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条回答
  •  青春惊慌失措
    2021-02-05 21:58

    In Codeigniter there are many way to pass the value from one controller to other.

    You can use codeigniter Session to pass the data from one controller to another controller.

    For that you have to first include the library for session

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

    Then You can set the flash data value using variable name.

    // Set flash data 
    $this->session->set_flashdata('variable_name', 'Value');
    

    Them you can get the value where you want by using the codeigniter session flashdata

    // Get flash data
    $this->session->flashdata('variable_name');
    

    Second Option codeigniter allow you to redirect the url from controll with controller name, method name and value and then you can get the value in another controller.

    // Passing the value 
    redirect('/another_controller_name/method_name/variable');
    

    Then you can get the value in another controller

    public function method_name($variable)
    {
    echo $variable;
    }
    

    That all....

提交回复
热议问题