【发布时间】:2012-01-11 02:13:55
【问题描述】:
我计划在 PHP 中为简单项目构建一个轻量级的简单 MVC。
如果是 AJAX 请求,我正在尝试掌握显示视图模板或返回 JSON 响应的简单路径。
下面是我刚想出的一个 sn-p,这都是假设的,所以实际上还没有任何方法存在,这正是我期望它们工作的方式。
查看下面的代码并阅读其中的 cmets,这看起来像您可能会返回 AJAX 请求而不是基本 MVC 中的视图模板吗?
/**
* Example Controller
*/
class Test_Controller extends Core_Controller {
function SomeAction($userId)
{
//GET ID from URI
$this->request->get($userId['id']);
// load a Model 'some' = Model Name
$this->profile_model = $this->loadModel('some');
// Get the Dataset from the Model
$profileData = $this->profile_model->getProfile($userId);
// Check if this is an AJAX request true/false
if($this->request->isAjax()){
//is AJAX is true then we return json instead of loading a View template
return $this->response->json($profileData);
}else{
// load view file
$this->view->load('userProfile', $profileData);
}
}
【问题讨论】: