Api-platform:如何输出相关的实体id而不是资源路径?
Api-platform: how to output related entity id instead of resource path?
如何获取相关实体 ID 而不是资源路径?
例如,这是我现在得到的:
{
"id": "/api/articles/0d8cb40c-221b-4c54-9e29-43877093b839",
"type": "Article",
"attributes": {
"_id": "0d8cb40c-221b-4c54-9e29-43877093b839",
"title": "Article",
"content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>",
"dateCreated": "2018-07-17T18:20:29+03:00",
"dateUpdated": "2018-07-17T18:20:29+03:00"
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": "/api/users/b26e0381-6800-4f47-b269-9a92b27ad331"
}
}
}
}
我想像这样获取相关的实体 ID:
"relationships": {
"author": {
"data": {
"type": "User",
"id": "b26e0381-6800-4f47-b269-9a92b27ad331"
}
}
您可以使用以下配置:
api_platform:
allow_plain_identifiers: true
但请注意:
- 不鼓励:超媒体 API 需要使用 URL 作为标识符
- 普通标识符处理将来可能会发生变化:https://github.com/api-platform/core/pull/2022#issuecomment-398851861
如何获取相关实体 ID 而不是资源路径?
例如,这是我现在得到的:
{
"id": "/api/articles/0d8cb40c-221b-4c54-9e29-43877093b839",
"type": "Article",
"attributes": {
"_id": "0d8cb40c-221b-4c54-9e29-43877093b839",
"title": "Article",
"content": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>",
"dateCreated": "2018-07-17T18:20:29+03:00",
"dateUpdated": "2018-07-17T18:20:29+03:00"
},
"relationships": {
"author": {
"data": {
"type": "User",
"id": "/api/users/b26e0381-6800-4f47-b269-9a92b27ad331"
}
}
}
}
我想像这样获取相关的实体 ID:
"relationships": {
"author": {
"data": {
"type": "User",
"id": "b26e0381-6800-4f47-b269-9a92b27ad331"
}
}
您可以使用以下配置:
api_platform:
allow_plain_identifiers: true
但请注意:
- 不鼓励:超媒体 API 需要使用 URL 作为标识符
- 普通标识符处理将来可能会发生变化:https://github.com/api-platform/core/pull/2022#issuecomment-398851861