我已经阅读了很多关于此的内容,但我仍然不确定我的实现是否正确,因为我没有来自应用程序的反馈......我已将以下代码添加到 routes.php
路由器::mapResources('posts'); 路由器::parseExtensions('json');
以及控制器 PostsController.php 上的以下代码
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
var $components = array('RequestHandler');
public function index() {
$this->set('posts', $this->Post->find('all'));
}
public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
$this->set(compact('posts'));
}
public function add() {
if ($this->request->is('post')) {
if ($this->Post->save($this->request->data)) {
$this->set(compact('posts'));
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
public function edit($id = null) {
$this->Post->id = $id;
if ($this->request->is('get')) {
$this->request->data = $this->Post->read();
} else {
if ($this->Post->save($this->request->data)) {
$this->set(compact('posts'));
$this->Session->setFlash('Your post has been updated.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to update your post.');
}
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Post->delete($id)) {
$this->set(compact('posts'));
$this->Session->setFlash('The post with id: ' . $id . ' has been deleted.');
$this->redirect(array('action' => 'index'));
}
}
}
这够了吗?我的目标是让一个 android 应用程序通过这个 web 服务与 MySQL 数据库通信。希望你能帮我 :/