Symfony 如何为子资源启用分页?
在API配置文件中,我禁用了分页:Symfony 如何为子资源启用分页?,symfony,api-platform.com,Symfony,Api Platform.com,在API配置文件中,我禁用了分页: api_platform.yaml: api_platform: collection: pagination: enabled: false 但我想为特定子资源启用分页: 父类: /** * @ORM\Entity(repositoryClass="App\Repository\ForumRepository") * @ApiResource( * collectionOpe
api_platform.yaml
:
api_platform:
collection:
pagination:
enabled: false
但我想为特定子资源启用分页:
父类:
/**
* @ORM\Entity(repositoryClass="App\Repository\ForumRepository")
* @ApiResource(
* collectionOperations={
* "get",
* },
* itemOperations={"get"},
* )
*/
class Forum
{
...
/**
* @var ForumSubject[]|ArrayCollection
*
* @ORM\OneToMany(
* targetEntity="App\Entity\ForumSubject",
* mappedBy="forum",
* )
* @ApiSubresource(maxDepth=1)
*/
private $subjects;
...
/**
* @ORM\Entity(repositoryClass="App\Repository\ForumSubjectRepository")
* @ApiResource(
* collectionOperations={
* "post",
* },
* itemOperations={
* "get",
* "put",
* "delete"
* },
* )
*/
class ForumSubject
{
...
儿童班:
/**
* @ORM\Entity(repositoryClass="App\Repository\ForumRepository")
* @ApiResource(
* collectionOperations={
* "get",
* },
* itemOperations={"get"},
* )
*/
class Forum
{
...
/**
* @var ForumSubject[]|ArrayCollection
*
* @ORM\OneToMany(
* targetEntity="App\Entity\ForumSubject",
* mappedBy="forum",
* )
* @ApiSubresource(maxDepth=1)
*/
private $subjects;
...
/**
* @ORM\Entity(repositoryClass="App\Repository\ForumSubjectRepository")
* @ApiResource(
* collectionOperations={
* "post",
* },
* itemOperations={
* "get",
* "put",
* "delete"
* },
* )
*/
class ForumSubject
{
...
一切正常我可以访问我的子资源:
/api/forums/a21cb5db-aed7-45a6-884f-3d3e6d7abd8c/主题
(返回论坛的所有主题,路径名称:api\u论坛\u主题\u获取\u子资源
)
但是我无法启用此路由上的分页,文档中没有提到任何内容
经过研究,我尝试了这个,但没有任何效果:
在父类或子类中不起作用:
* @ApiResource(
* ...
* subresourceOperations={
* "api_forums_subjects_get_subresource"={"pagination_enabled"=true}
* }
在父类中不起作用;在子类中出错(在Swagger规范中找不到操作“api\u论坛\u主题\u获取\u子资源”)
查看API平台源代码后,以下是解决方案: 在儿童班中:
* @ApiResource(
* ...
* collectionOperations={
* ...
* "api_forums_subjects_get_subresource"={
* "method"="get", // don't forget : otherwise trigger a strange swagger error
* "pagination_enabled"=true
* }
查看API平台源代码后,以下是解决方案: 在儿童班中:
* @ApiResource(
* ...
* collectionOperations={
* ...
* "api_forums_subjects_get_subresource"={
* "method"="get", // don't forget : otherwise trigger a strange swagger error
* "pagination_enabled"=true
* }