2012-04-17 57 views
1

我已阅读了很多关于此,但我仍然不确定如果我的实现是正确的或没有,因为我没有从应用程序的反馈...我已经将以下代码添加到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

你是什么意思“是不够?” ?只有你可以回答。如果它满足你的需求,那就足够了。否则,请编辑您的问题以更具体。发布示例网址以及您收到的输出或错误。 (另外,请确保您的调试> 0)。 – Costa 2012-04-19 06:38:15

+0

事情是我不会如何将代码放在该控制器上准备接收和发送请求到Android应用程序。我可以找到关于该 – Bixtape 2012-04-19 20:24:53

+0

的任何信息忘记现在的Android应用程序。只需手动键入URL并查看是否获得了正确的JSON响应。您还可以使用“REST Client”浏览器插件进行测试。在Firefox上,我使用HTTPFox来查看返回的响应/头文件。 – Costa 2012-04-19 23:01:19

回答

0

感谢科斯塔我能获得剩下的工作(我认为XD)我用一个撑着,他建议测试,但我只能从http://localhost:3030/cake_2_1/posts/index.json收到一个正确的答案 他们有POST/recipes.format RecipesController :: add()来添加一些东西,但是如何转换成一个url在这里? echo json_encode($message);是我在视图\ Posts \ json \ add.ctp和

if ($this->Post->save($this->request->data)) { $message = 'Saved'; } else { $message = 'Error'; } $this->set(compact("message")); 是我在控制器添加功能。几乎完成只是需要更多的帮助:)

+0

POST的URL与GET相同。不同的是HTTP方法使用'POST'而不是'GET'。您无法单独通过URL测试POST。您必须将表单发布到'/ posts.json' URL或使用REST客户端浏览器插件来创建对'/ posts.json'的POST请求。 REST很难以这种方式进行测试。就个人而言,我不使用'mapresources()',并且让所有事情都通过GET来完成,所以我可以在浏览器中通过书签URL来测试。这是“反REST”,但它让我测试变得更加容易,最后它仍然可以正常工作。 – Costa 2012-04-20 07:25:02

+0

那么你用什么?你怎么测试add()和edit(id)方法? 我已将此添加到routes.php 'Router :: connect( “/:controller/title/body /:id”, array(“action”=>“edit”,“[method]”= >“PUT”), \t array(“title”=>“[az] +”), \t array(“body”=>“[az] +”), array(“id”=>“[ 0-9] +“) \t);' 但它不工作... – Bixtape 2012-04-20 12:47:04

+0

我测试添加/编辑通过张贴表单到一个网址(一个控制器/动作创建的形式是张贴到另一个控制器/动作) 。我不使用任何路线,它会混淆你并产生问题。 – Costa 2012-04-21 07:15:09