symfony - 如何为子资源启用分页?
问题描述
在我的 API 配置文件中,我禁用了分页:
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
{
...
一切正常我可以访问我的子资源:(
/api/forums/a21cb5db-aed7-45a6-884f-3d3e6d7abd8c/subjects
返回论坛的所有主题,路线名称:)api_forums_subjects_get_subresource
。
但我无法在这条路线上启用分页,文档没有提及任何内容。
经过研究,我尝试了这个,但没有任何效果:
不适用于父类或子类:
* @ApiResource(
* ...
* subresourceOperations={
* "api_forums_subjects_get_subresource"={"pagination_enabled"=true}
* }
不适用于父类;错误 un children 类(在 Swagger 规范中找不到操作“api_forums_subjects_get_subresource”)。
* @ApiResource(
* ...
* collectionOperations={
* "get",
* "api_forums_subjects_get_subresource"={"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
* }
推荐阅读
- spring-boot - 如何在 SpringBoot 中将 127.0.0.1 映射到 example.com
- php - ftp_get 不适用于重音字符
- mysql - 无法浏览 phpMyAdmin 表:错误 #1038 - 排序内存不足,请考虑增加服务器排序缓冲区大小
- angular - 通过路径上的状态传递的角度视图数据
- c++ - 如何删除 C++ 中类的所有隐式(默认)方法?
- java - 在 Java 中读取 .txt 文件
- java - 更改日志记录级别以从使用索引参数的查询中打印实际值
- r - geom_rect 给出没有矩形的空图
- kotlin - 使用 Gson 反序列化后计算的瞬态值为空
- jenkins - 在 Jenkins 作业中列出 RPM