Symfony - Api-平台框架:获取用户的所有项目
Symfony - Api-Platform framework: Get all items of a user
我是一名年轻的开发人员,我最近发现 Api-Platform 可以充分休息 api。
目前我在关注doc,但我不明白如何通过示例获取一个人的所有书籍。
我在这里:
我有自动生成的 Book 和 Person 实体,我只是在它们之间添加了 ManyToMany 关系。
然后我得到以下结果:
获取api.platform.dev/app_dev.php/
{
@context: "/app_dev.php/contexts/Entrypoint",
@id: "/app_dev.php/",
@type: "Entrypoint",
person: "/app_dev.php/people",
book: "/app_dev.php/books"
}
获取api.platform.dev/app_dev.php/people/3
{
@context: "/app_dev.php/contexts/Person",
@id: "/app_dev.php/people/3",
@type: "http://schema.org/Person",
birthDate: null,
description: "test",
gender: "Femme",
name: "test",
url: null,
books: [
"/app_dev.php/books/1",
"/app_dev.php/books/4"
]
}
这是我的问题,如何在第二个结果中获得一个人所有书籍的超媒体,以及获得一个人所有书籍的最佳选择是什么?
我已经开始创建我的自定义操作:
#services.yml
resource.person.item_operation.custom_get:
class: "Dunglas\ApiBundle\Api\Operation\Operation"
public: false
factory:
- "@api.operation_factory"
- "createItemOperation"
arguments:
- "@resource.person" # Resource
- ["GET"] # Methods
- "/people/{id}/books" # Path
- "AppBundle:Person:custom" # Controller
- "my_custom_route2" # Route name
- # Context (will be present in Hydra documentation)
"@type": "hydra:Operation"
"hydra:title": "A custom operation"
"returns": "xmls:string"
和
// PersonController.php
<?php
namespace AppBundle\Controller;
use Dunglas\ApiBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
class PersonController extends ResourceController
{
public function customAction(Request $request, $id)
{
return parent::getAction($request, $id);
}
}
GET 的结果api.platform.dev/app_dev.php/people/3/books
和基本的一样api.platform.dev/app_dev.php/people/3
,正常我叫父
但是现在最好的方法是什么:
# GET api.platform.dev/app_dev.php/people/3/books
{
@context: "/app_dev.php/contexts/Book",
@id: "/app_dev.php/people/3/books",
@type: "hydra:PagedCollection",
hydra:totalItems: 2,
hydra:itemsPerPage: 30,
hydra:firstPage: "/app_dev.php/people/3/books",
hydra:lastPage: "/app_dev.php/people/3/books",
hydra:member: [
{
@id: "/app_dev.php/books/1",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
},
{
@id: "/app_dev.php/books/2",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
}
]
}
当我得到 api.platform.dev/app_dev.php/people/3
添加这个 IRI /app_dev.php/people/3/books
谢谢你能给我的帮助。
像这样创建子 collection 是可行的,但对于 API 平台的 v1 来说很复杂,因为它需要创建大量自定义 类(使用v2).
我建议使用以下结构:
GET /books?people=/people/3
: 检索 people 3
拥有的所有书籍
使用the builtin API Platform search filter即可轻松完成。
然后,如果您想要超媒体支持(顺便说一句,您真的需要它吗 - 我的意思是,您的客户端是否能够取消引用此类超媒体 links?),您需要 create a custom Symfony normalizer 装饰Dunglas\ApiBundle\JsonLd\Serializer\ItemNormalizer
(服务 api.json_ld.normalizer.item
)由 API 平台提供,并为 books
属性 添加 link 到 /books?people=/people/3
。
如果你想继续沿着你开始的道路前进,你需要使用Dunglas\ApiBundle\Hydra\Serializer\CollectionNormalizer
将你的collection 规范化为Hydra 格式。您仍然可以装饰 ItemNormalizer 以指向您的自定义 collection URL.
希望对您有所帮助。
我是一名年轻的开发人员,我最近发现 Api-Platform 可以充分休息 api。
目前我在关注doc,但我不明白如何通过示例获取一个人的所有书籍。
我在这里:
我有自动生成的 Book 和 Person 实体,我只是在它们之间添加了 ManyToMany 关系。
然后我得到以下结果:
获取api.platform.dev/app_dev.php/
{
@context: "/app_dev.php/contexts/Entrypoint",
@id: "/app_dev.php/",
@type: "Entrypoint",
person: "/app_dev.php/people",
book: "/app_dev.php/books"
}
获取api.platform.dev/app_dev.php/people/3
{
@context: "/app_dev.php/contexts/Person",
@id: "/app_dev.php/people/3",
@type: "http://schema.org/Person",
birthDate: null,
description: "test",
gender: "Femme",
name: "test",
url: null,
books: [
"/app_dev.php/books/1",
"/app_dev.php/books/4"
]
}
这是我的问题,如何在第二个结果中获得一个人所有书籍的超媒体,以及获得一个人所有书籍的最佳选择是什么?
我已经开始创建我的自定义操作:
#services.yml
resource.person.item_operation.custom_get:
class: "Dunglas\ApiBundle\Api\Operation\Operation"
public: false
factory:
- "@api.operation_factory"
- "createItemOperation"
arguments:
- "@resource.person" # Resource
- ["GET"] # Methods
- "/people/{id}/books" # Path
- "AppBundle:Person:custom" # Controller
- "my_custom_route2" # Route name
- # Context (will be present in Hydra documentation)
"@type": "hydra:Operation"
"hydra:title": "A custom operation"
"returns": "xmls:string"
和
// PersonController.php
<?php
namespace AppBundle\Controller;
use Dunglas\ApiBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
class PersonController extends ResourceController
{
public function customAction(Request $request, $id)
{
return parent::getAction($request, $id);
}
}
GET 的结果api.platform.dev/app_dev.php/people/3/books
和基本的一样api.platform.dev/app_dev.php/people/3
,正常我叫父
但是现在最好的方法是什么:
# GET api.platform.dev/app_dev.php/people/3/books
{
@context: "/app_dev.php/contexts/Book",
@id: "/app_dev.php/people/3/books",
@type: "hydra:PagedCollection",
hydra:totalItems: 2,
hydra:itemsPerPage: 30,
hydra:firstPage: "/app_dev.php/people/3/books",
hydra:lastPage: "/app_dev.php/people/3/books",
hydra:member: [
{
@id: "/app_dev.php/books/1",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
},
{
@id: "/app_dev.php/books/2",
@type: "http://schema.org/Book",
illustrator: [ ],
isbn: null,
numberOfPages: 1230,
author: [ ],
datePublished: null,
description: "Desription",
genre: null,
name: "someone",
publisher: null
}
]
}
当我得到 api.platform.dev/app_dev.php/people/3
添加这个 IRI /app_dev.php/people/3/books
谢谢你能给我的帮助。
像这样创建子 collection 是可行的,但对于 API 平台的 v1 来说很复杂,因为它需要创建大量自定义 类(使用v2).
我建议使用以下结构:
GET /books?people=/people/3
: 检索 people 3
使用the builtin API Platform search filter即可轻松完成。
然后,如果您想要超媒体支持(顺便说一句,您真的需要它吗 - 我的意思是,您的客户端是否能够取消引用此类超媒体 links?),您需要 create a custom Symfony normalizer 装饰Dunglas\ApiBundle\JsonLd\Serializer\ItemNormalizer
(服务 api.json_ld.normalizer.item
)由 API 平台提供,并为 books
属性 添加 link 到 /books?people=/people/3
。
如果你想继续沿着你开始的道路前进,你需要使用Dunglas\ApiBundle\Hydra\Serializer\CollectionNormalizer
将你的collection 规范化为Hydra 格式。您仍然可以装饰 ItemNormalizer 以指向您的自定义 collection URL.
希望对您有所帮助。