使用PHP的EmberJSAPI问题
我正在使用Laravel/PHP输出一些JSON供Ember拾取。。。。这里有几件事 首先,我的PHP看起来像这样(是否有其他方式发送数据) 这是我习惯做的事使用PHP的EmberJSAPI问题,php,ember.js,Php,Ember.js,我正在使用Laravel/PHP输出一些JSON供Ember拾取。。。。这里有几件事 首先,我的PHP看起来像这样(是否有其他方式发送数据) 这是我习惯做的事 foreach($articles as $article) { $response['articles'][] = [ 'id' => $article->id, 'body' => $article->body,
foreach($articles as $article) {
$response['articles'][] = [
'id' => $article->id,
'body' => $article->body,
'title' => $article->title
];
}
return Response::json([
'articles' => $articles->toArray()
], $statusCode);
第一个PHP代码段可以正常工作,但第二个不能。我得到了关于资源类型的各种错误
下一个问题是余烬头。现在我正在使用RESTAdapter
进行所有工作,但是我应该使用JSONAPIAdapter
吗?当我试图让它与JSONAPIAdapter
和JSONAPISerializer
一起工作时,我得到了这个错误
必须存在以下一个或多个键:\“data\”,
\“错误\”,“元”
。我可以消除该错误,但随后我会得到一个关于未定义类型或未知资源的错误。使用JSONAPIAdapter不是强制性的,但如果您可以控制API,则可以很好地使用它。API响应应遵循以下格式() 单个资源对象的示例格式
{
"data": {
"type": "articles",
"id": "1",
"attributes": {
// ... this article's attributes
}
}
}
多个资源对象的示例格式
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
}, {
"type": "articles",
"id": "2",
"attributes": {
"title": "Rails is Omakase"
}
}]
}
显示第二个代码段的
return
部分。第二个问题-jsonapi必须包含data
键作为“主要数据”我更新了帖子以显示返回用户1156168…我在数组中有这个,但我认为我的PHP是错误的,我会再次测试,看看我能找到什么。你认为我需要一个序列化程序,或者在这个例子中这会是过度的吗?非常好的kumkanillam,谢谢你的回答。该格式与JSONAPI完美配合!
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
}
}, {
"type": "articles",
"id": "2",
"attributes": {
"title": "Rails is Omakase"
}
}]
}