Search code examples
api-platform.com

Api-platform: how to output related entity id instead of resource path?


How can I get the related entity Id instead of resource path?

For example, here's what I get now:

{
  "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"
      }
    }
  }
}

I would like to get related entity id like this:

"relationships": {
"author": {
  "data": {
    "type": "User",
    "id": "b26e0381-6800-4f47-b269-9a92b27ad331"
  }
}

Solution

  • API Platform 3 deprecated the allow_plain_identifiers option, but there is now a documentation page dedicated to this topic.

    With API Platform 2, you can use the following configuration:

    api_platform:
        allow_plain_identifiers: true
    

    But note that: