简体   繁体   English

Symfony-Api平台框架:获取用户的所有项目

[英]Symfony - Api-Platform framework: Get all items of a user

I'm a young developer and I recently discover Api-Platform to make a full rest api. 我是一名年轻的开发人员,最近发现Api平台可以制作完整的api。

Currently, I'm following the doc , but I don't understand how to get all books of a person by example. 目前,我正在关注doc ,但是我不了解如何通过示例获取某人的所有书籍。

Here I am: 我在这里:

I have Book and Person entities auto generated, I just add a ManyToMany relation between them. 我有自动生成的Book和Person实体,我只是在它们之间添加了ManyToMany关系。

Then I have the following results: 然后,我得到以下结果:

GET api.platform.dev/app_dev.php/ GET 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"
}

GET api.platform.dev/app_dev.php/people/3 GET 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"
    ]
}

Here is my questions, How can I get in the second result an hypermedia to all books of the person, and What is the best option to get all books of a person ? 这是我的问题,如何在第二个结果中获得该人所有书籍的超媒体,以及获得一个人所有书籍的最佳选择是什么?

I have started with create my custom operation: 我已经开始创建自定义操作:

#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"

and

// 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);
    }
}

result on GET api.platform.dev/app_dev.php/people/3/books is the same of the basic api.platform.dev/app_dev.php/people/3 , normal I call the parent. GET api.platform.dev/app_dev.php/people/3/books上的结果与基本api.platform.dev/app_dev.php/people/3相同,正常情况下我称为父级。

But now what is the best way to have something like this: 但是,现在拥有这样的最好方法是什么:

# 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
        }
    ]
}

And when I get api.platform.dev/app_dev.php/people/3 add this IRI /app_dev.php/people/3/books 当我得到api.platform.dev/app_dev.php/people/3添加此IRI /app_dev.php/people/3/books

Thanks you for the help you could give me. 谢谢您能给我的帮助。

Creating sub collections like this is doable but complex with the v1 of API Platform as it requires to create a lot of custom classes (it will be easier with the v2). 使用API​​ Platform v1创建这样的子集合是可行的,但很复杂,因为它需要创建许多自定义类(使用v2会更容易)。

What I would suggest is to use the following structure: 我建议使用以下结构:

GET /books?people=/people/3 : retrieve all books owned bu the people 3 GET /books?people=/people/3 :检索GET /books?people=/people/3拥有的所有书籍

It can be done easily using the builtin API Platform search filter . 使用内置的API平台搜索过滤器可以轻松完成此操作。

Then, if you want hypermedia support (btw do you really need it - I mean, is your client able to dereference such hypermedia links?), you need can create a custom Symfony normalizer decorating the Dunglas\\ApiBundle\\JsonLd\\Serializer\\ItemNormalizer (service api.json_ld.normalizer.item ) provided by API Platform and adding a link to /books?people=/people/3 for the books property. 然后,如果您想要超媒体支持(顺便说一句,您是否真的需要它-我的意思是,您的客户端是否可以取消引用此类超媒体链接?),您需要创建一个自定义的Symfony规范化器,装饰Dunglas\\ApiBundle\\JsonLd\\Serializer\\ItemNormalizer ( API平台提供的service api.json_ld.normalizer.item ),并为books属性添加到/books?people=/people/3的链接。

If you want to still want to follow the road you started, you need to use the Dunglas\\ApiBundle\\Hydra\\Serializer\\CollectionNormalizer to normalize your collection in the Hydra format. 如果您仍然想遵循开始的道路,则需要使用Dunglas\\ApiBundle\\Hydra\\Serializer\\CollectionNormalizer以Hydra格式规范化您的收藏。 You can still decorate the ItemNormalizer to point to your custom collection URL. 您仍然可以装饰ItemNormalizer以指向您的自定义集合URL。

I hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM