【发布时间】:2014-01-22 05:15:23
【问题描述】:
我为“应用程序”表创建了一个控制器。 Web 和 REST 界面可以正常工作,但我认为添加和编辑功能应该更好。
当我测试添加和编辑时,我发现需要以 web FORM 格式(而不是 JSON)发布数据。
我发现我需要在保存中使用“$this->request->input('json_decode')”来解码 JSON 数据。我以为这是自动发生的。
此函数现在适用于添加(编辑类似)并显示我的 json/add.ctp 以便我可以将成功记录返回给用户。
public function add() {
if ($this->request->is('post')) {
$this->Application->create();
//Is the request REST passing a JSON object?
if (preg_match('/\.json/', $this->request->here)){
//This is a REST call
$this->set('status', $this->Application->save($this->request->input('json_decode')));
} else {
//This is an interactive session
if ($this->Application->save($this->request->data)) {
$this->Session->setFlash(__('The application has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The application could not be saved. Please, try again.'));
}
}
}
}
我使用“$this->request->here”来查看它是否以“.json”结尾。这是处理 REST 调用的“正确”方式吗?
【问题讨论】:
标签: rest cakephp cakephp-2.4