What is the best practice for a model to access data from a controller in Codeigniter?
The best practice for a model to access data from a controller in Codeigniter is to pass the data to the model as a parameter when calling a model method from the controller. This ensures separation of concerns and follows the MVC pattern.
// Controller
$data = array('name' => 'John', 'email' => 'john@example.com');
$this->load->model('User_model');
$this->User_model->save_user_data($data);
// Model
class User_model extends CI_Model {
public function save_user_data($data) {
$this->db->insert('users', $data);
}
}