微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为子资源启用分页?

如何解决如何为子资源启用分页?

在我的 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 子类(在 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
 *         }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。