Yes, you can post data from one controller to another in Codeigniter 3.
Here are a few ways to achieve this:
1. Using Redirect with Flashdata
You can use Codeigniter's built-in flashdata to pass data between controllers. Here's an example:
// Controller 1
public function method1()
{
$data = array('key' => 'value');
$this->session->set_flashdata('data', $data);
redirect('controller2/method2');
}
// Controller 2
public function method2()
{
$data = $this->session->flashdata('data');
// Use the data
}
2. Using Session
You can also use Codeigniter's session library to store and retrieve data between controllers.
// Controller 1
public function method1()
{
$data = array('key' => 'value');
$this->session->set_userdata('data', $data);
redirect('controller2/method2');
}
// Controller 2
public function method2()
{
$data = $this->session->userdata('data');
// Use the data
$this->session->unset_userdata('data'); // Don't forget to unset the data
}
3. Using GET Request
If the data is not sensitive, you can pass it as a GET request.
// Controller 1
public function method1()
{
$data = array('key' => 'value');
redirect('controller2/method2?' . http_build_query($data));
}
// Controller 2
public function method2()
{
$data = $this->input->get();
// Use the data
}
4. Using POST Request with CURL
You can use CURL to send a POST request from one controller to another.
// Controller 1
public function method1()
{
$data = array('key' => 'value');
$url = 'controller2/method2';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
}
// Controller 2
public function method2()
{
$data = $this->input->post();
// Use the data
}
Remember to always validate and sanitize user input data to prevent security vulnerabilities.