0

首先我想实现JSON API。我遵循api 平台上的教程,就像在示例中创建实体和响应一样

{
    "links": {
        "self": "/api/books"
    },
    "meta": {
        "totalItems": 1,
        "itemsPerPage": 30,
        "currentPage": 1
    },
    "data": [
        {
            "id": "/api/books/1",
            "type": "Book",
            "attributes": {
                "isbn": "9781782164104",
                "title": "Persistence in PHP with the Doctrine ORM",
                "description": "This book is designed for PHP developers and architects who want to modernize their skills through better understanding of Persistence and ORM.",
                "author": "Kévin Dunglas",
                "publicationDate": "2013-12-01T00:00:00+01:00",
                "_id": 1
            },
            "relationships": {
                "reviews": {
                    "data": [
                        {
                            "type": "Review",
                            "id": "/api/reviews/1"
                        }
                    ]
                }
            }
        }
    ]
}

我的api_platform.yaml配置

api_platform:
    mapping:
        paths: ['%kernel.project_dir%/src/Entity']
    formats:
        jsonapi:
            mime_types: ['application/vnd.api+json']

所以我在id提交时遇到了问题data。我得到id格式的字段,api/entityName/id但我只想得到数字(字符串),就像在 JSON API中一样。那么是否有一些我想念的配置或者有什么方法可以实现

4

1 回答 1

0

在这里讨论过。您需要使用 Normalizer 或创建自定义 getter。

于 2018-02-22T07:58:45.883 回答