Sometimes i need data like array and sometimes i need same data as json.
Where would you do the check if is a ajax call, in controller or model or… Which one is better?
Test if is ajax call in controller
function my_controller(){
//getdata from model
$data=$this->my_model();
if(THIS_IS_AJAX_CALL){
echo json_encode($data);
}else{
return $data;
}
}
function my_model(){
//get the data from db
return $data;
}
Pass type as argument to model:
function my_controller(){
if(THIS_IS_AJAX_CALL){
return $this->my_model('json');
}else{
return $this->my_model();
}
}
function my_model($type=''){
//get the data from db
if($type='json'){
return json_encode($data);
}else{
return $data;
}
}
The controller. The model does not care how the data needs to be represented to the user, only the data itself.