我已阅读了很多关于此,但我仍然不确定如果我的实现是正确的或没有,因为我没有从应用程序的反馈...我已经将以下代码添加到routes.php文件cakePhP REST配置
Router::mapResources('posts'); Router::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应用程序通过这个webservice与MySQL数据库进行通信。 希望你能帮助我:/
你是什么意思“是不够?” ?只有你可以回答。如果它满足你的需求,那就足够了。否则,请编辑您的问题以更具体。发布示例网址以及您收到的输出或错误。 (另外,请确保您的调试> 0)。 – Costa 2012-04-19 06:38:15
事情是我不会如何将代码放在该控制器上准备接收和发送请求到Android应用程序。我可以找到关于该 – Bixtape 2012-04-19 20:24:53
的任何信息忘记现在的Android应用程序。只需手动键入URL并查看是否获得了正确的JSON响应。您还可以使用“REST Client”浏览器插件进行测试。在Firefox上,我使用HTTPFox来查看返回的响应/头文件。 – Costa 2012-04-19 23:01:19