Json cakePhP REST配置
我已经读了很多关于这方面的书,但我仍然不确定我的实现是否正确,因为我没有来自应用程序的反馈。。。我已经将以下代码添加到routes.php 路由器::mapResources('posts'); 路由器::parseExtensions('json') 以及控制器PostsController.php上的以下代码Json cakePhP REST配置,json,rest,cakephp-2.0,Json,Rest,Cakephp 2.0,我已经读了很多关于这方面的书,但我仍然不确定我的实现是否正确,因为我没有来自应用程序的反馈。。。我已经将以下代码添加到routes.php 路由器::mapResources('posts'); 路由器::parseExtensions('json') 以及控制器PostsController.php上的以下代码 class PostsController extends AppController { public $helpers = array('Html', 'Form'); var $
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数据库通信。
希望你能帮助我:/多亏了科斯塔,我才能够让其余的工作正常(我想XD)我使用了他建议的一个插件来测试它,但我只能从他那里得到正确的答案 在coockbook中,他们有POST/recipes.format recipes controller::add()来添加内容,但如何将其转换为url呢<代码>回声json_编码($message)是我在View\Posts\json\add.ctp和
if($this->Post->save($this->request->data)){
$message='Saved';
}否则{
$message='Error';
}
$this->set(压缩(“消息”);
是我在控制器添加函数中拥有的。差不多完成了,只需要多一点帮助:)你说的“够了吗?”?只有你能回答这个问题。如果它能满足你的需要,那就足够了。否则,请编辑您的问题,使其更具体。发布一个示例URL以及返回的输出或错误。(另外,确保你的
debug>0
)问题是我不知道如何将代码放在控制器上,以便接收和发送请求到android应用程序。我可以找到任何关于它的信息,暂时忘记android应用程序吧。只需手动输入URL,看看是否得到正确的JSON响应。您还可以使用“REST客户端”浏览器插件进行测试。在Firefox上,我使用HTTPFox查看返回的响应/标题。键入url localhost:3030/cake_2_1/posts.get?/posts.json
。顺便说一句,键入的URL总是“GET”。如果你想测试PUT/DELETE/etc,可以使用我提到的插件。POST的URL与GET的URL相同。区别在于使用的HTTP方法是POST
,而不是GET
。你不能仅仅通过URL测试帖子。您必须实际将表单发布到/posts.json
URL,或者使用REST客户端浏览器插件创建对/posts.json
的post请求。用这种方法测试REST是相当困难的。就我个人而言,我不使用mapresources()
,我让一切都通过GET,这样我就可以通过浏览器中的书签URL进行测试。这是“反REST”,但它让我更容易测试,而且最终,它都能正常工作。那么你用什么呢?如何测试add()和edit(id)方法?我已经将其添加到routes.phpRouter::connect(“/:controller/title/body/:id”、数组(“action”=>“edit”、“[method]”=>“PUT”)、数组(“title”=>“[a-z]+”、数组(“body”=>“[a-z]+”、数组(“id”=>“[0-9]+”))代码>但它不工作…我通过将表单发布到url来测试添加/编辑(一个控制器/操作创建表单,该表单发布到另一个控制器/操作)。我不使用任何路线,这只会让你感到困惑并产生问题。