【发布时间】:2011-06-10 02:46:54
【问题描述】:
有时我需要数组之类的数据,有时我需要与 json 相同的数据。
您会在哪里检查是否是 ajax 调用,在控制器或模型中,还是...哪个更好?
测试是否是控制器中的ajax调用
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;
}
将类型作为参数传递给模型:
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;
}
}
【问题讨论】:
标签: php ajax oop json model-view-controller