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

6

14

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) ?

Any help would be appreciated .

Thanks in Advance :)

Teledu answered 10/10, 2012 at 9:54 Comment(1)
store the variable in sessionDavisson
S
17

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.

Smoker answered 10/10, 2012 at 10:14 Comment(2)
@Aditi Have you initialised the session library in the second controller as well?Smoker
Do you want to post what you've written in your question and I'll take a look? As far as I know it should work, so I can only assume there's a bug in your code.Smoker
S
7

I think in codeigniter you can't pass variable, between two different controller. One obvious mechanism is to use session data.

Schoolgirl answered 10/10, 2012 at 9:58 Comment(1)
I agree on that. CI has specific routing where controllers are accessed through URI. session data would be useful here $this->sessions->set_userdata('data',$data); redirect('second_cont.php');Foamflower
D
7

Ok, here is something about MVC most will readily quote:

A Controller is for taking input, a model is for your logic, and, a view is for displaying.

Now, strictly speaking you shouldn't want to send data from a controller to another. I can't think of any cases where that is required.


But, if it is absolutely needed, then you could simply use redirect to just redirect to the other controller.

Something like:

// some first_cont.php code here
redirect('/second_cont/valuereciever/value1')


// some second_cont.php code here
public function valureciever($value){
    echo $value; // will output value1
}
Doubletongued answered 10/10, 2012 at 9:59 Comment(0)
S
4

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....

Sudoriferous answered 6/3, 2014 at 9:29 Comment(3)
Your answer doesn't change anything , Its already answered above and Identical to existing answer.Infringement
@Farhad, I don't agree. This answer shows both ways to pass variable: those described by Matthew Daly and kalpesh patel as well as Prasanth answer. In fact, I'm not sure if this is not the best answer for me.Maidinwaiting
what if the user refreshes the page? the flash data variable no longer exists so... the code will failDall
B
2

If you are using session in the first controller then dont unset that session in first controller, instead store the value which you want in the other controller like,

$sess_array = array('value_name1' => 'value1', 'value_name2' => 'value2');
$this->session->set_userdata('session_name', $sess_array);

then reload this session in the other controller as

$session_data= $this->session->userdata('session_name');
$any_var_name = $session_data['value1'];
$any_var_name = $session_data['value2'];

this is how you can pass values from one controller to another....

Basilica answered 8/9, 2015 at 13:11 Comment(0)
B
0

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. %@$)

Betelgeuse answered 9/7, 2020 at 12:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.