我的REST的GET,DELETE但被工作PUT(编辑),奇怪的是在不更新数据库:
对于后期编辑车把模板:
<script type="text/x-handlebars" id="post">
<h1>View/Update Posts</h1>
{{#if isEditing}}
<p>Title: {{input type="text" value=title}}</p>
<p>Author: {{input type="text" value=author}}</p>
<p>Body: {{textarea value=body}}</p>
<button {{action 'doneEditing' this}}>Done</button>
{{else}}
<p>Title : {{title}}</p>
<p>Author: {{author}}</p>
<p>Body : {{body}}</p>
<button {{action 'edit'}}>Edit</button>
{{/if}}
在PostController中
App.PostController = Ember.ObjectController.extend({
isEditing: false,
actions: {
edit: function() {
this.set('isEditing', true);
},
doneEditing: function(post) {
this.set('isEditing', false);
post.save(); //NOT WORKING - NOT UPDATING THE DATABASE RECORD!!!
}
}
});
REST接口和数据模型
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
body: DS.attr('string')
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'emberpostsrest/api'
});
灰烬应用程序URL
http://localhost/emberpostsrest
REST风格的服务器(使用PHP SLIM)
http://localhost/emberpostsrest/api
工作REST
http://localhost/emberpostsrest/api/posts (GET all)
http://localhost/emberpostsrest/api/posts/1 (GET via id)
http://localhost/emberpostsrest/api/posts/1 (DELETE via id)
对于编辑REST风格的API使用PHP卷曲已经测试和工作正常:
//PUT - update
$data = array("id" => 3, "title" => "3", "author" => "2", "body" => "1");
$data_string = json_encode($data);
$ch = curl_init('http://localhost:8080/emberpostsrest/api/posts/3');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
修身的PHP代码
$app->put('/posts/:id', 'updatePostByID'); //update post via id
function updatePostByID($id)
{
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$post = json_decode($body);
$sql = "UPDATE posts
SET title = :title,
author = :author,
body = :body
WHERE id = :id";
try
{
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $post->id);
$stmt->bindParam("title", $post->title);
$stmt->bindParam("author", $post->author);
$stmt->bindParam("body", $post->body);
$stmt->execute();
}
catch(PDOException $e)
{
$errorMessage = $e->getMessage();
}
}
感谢您的帮助:d