简体   繁体   English

API 带有自定义请求正文的平台自定义资源

[英]API Platform custom resource with custom request body

I'm pretty new to Symfony and API Platform.我对 Symfony 和 API 平台还很陌生。 I'm trying to create a custom resource/operation which receives the body of the request, fetches data from the database according to the values and then returns a collection of the entity back to the client.我正在尝试创建一个自定义资源/操作,它接收请求的主体,根据值从数据库中获取数据,然后将实体的集合返回给客户端。

Example: A GET request with the following body:示例:具有以下正文的GET请求:

{
   "trackName": "example"
   "anotherKey": "anotherValue"
}

According to these attributes I would like to query data, do calculations and then return the data.根据这些属性我想查询数据,做计算,然后返回数据。

I'm trying to figure out how to make this the API Platform way.我试图弄清楚如何使这个成为 API 平台方式。 I've been googling and looking for it in the documentation for hours, but I can't find a way to achieve this.我一直在谷歌搜索并在文档中寻找它几个小时,但我找不到实现这一目标的方法。

I've tried to query data with a CollectionDataProvider and with a controller like this:我尝试使用CollectionDataProvider和 controller 查询数据,如下所示:

/**
 * @ApiResource(
 *     itemOperations={
 *          "tracks_custom_endpoint"={
 *              "method"="GET",
 *              "path"="/tracks/custom_endpoint",
 *              "controller"=MyController::class,
 *              "read"=false
 *          }
 *     }
 * )
 * @ORM\Entity(repositoryClass=SpotifyTrackRepository::class)
 */
class Track
{
   // Attributes, setters and getters
}

However, either way I can't access the body of the request respectively the JSON data which has been sent with it.但是,无论哪种方式,我都无法分别访问与它一起发送的 JSON 数据的请求正文。 Is there a way I can access this data somehow and then process it.有没有办法我可以以某种方式访问这些数据然后处理它。

First of all, consider that itemOperations specify endpoints that interact with a single resource .首先,考虑itemOperations指定与单个资源交互的端点。 Whereas collectionOperations specify endpoints that interact with the entire resource collection .collectionOperations指定与整个资源集合交互的端点。 Sounds like your endpoint should be configured under collectionOperations as you are seeking to return a filtered resource collection, not a single resource.听起来您的端点应该在collectionOperations下配置,因为您正在寻求返回过滤的资源集合,而不是单个资源。

Secondly, it is unusual for collection GET operations to expect a request body payload in order to provide collection filters .其次,集合GET操作期望请求正文有效负载以提供集合过滤器是不寻常的。 These are more commonly provided using query parameters.这些通常使用查询参数提供。 API Platform ships with many query parameter filters out of the box that cover most use cases (yours included). API 平台附带了许多开箱即用的查询参数过滤器,涵盖了大多数用例(包括您的用例)。 Otherwise use Extensions for more custom and complex collection filtering.否则,使用Extensions进行更多自定义和复杂的集合过滤。

Finally, there are Data Transformers that allow you to control how input (request payload) is transformed into resources, or how resources are transformed into output (response payload).最后,还有数据转换器,可让您控制如何将输入(请求负载)转换为资源,或者如何将资源转换为 output(响应负载)。 This seems to be what you are looking for.这似乎是您正在寻找的。 However, I suggest considering my previous points before going down this path.但是,我建议在走这条路之前考虑我以前的观点。

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

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