如何在 YAML 中使用 API 平台过滤器?
How can I use API Platform filters in YAML?
首先,我想通知你,我已经阅读了关于我今天遇到的问题的文档,我也在 Whosebug 和 Google 上进行了搜索,但没有找到正确的解决方案。
为了练习 Symfony 4 和 API 平台,我正在构建客户关系管理 API。我已经了解了很多关于 API 平台的知识,尽管我可以使用注释来配置资源,但我还是强迫自己使用 YAML 来了解更多信息。
因此,我有一个具有多个属性的客户资源:id、firstname、lastname、emailAddress、company、createdAt、updatedAt ,以及 所有者。我的目标是在此资源(在 YAML 中)上添加 SearchFilter 并将大部分属性添加为此过滤器的属性。
我已经尝试过这里给出的解决方案:https://github.com/api-platform/core/issues/1755。问题是我无法让它工作。在 services.yaml 中,我看到您可以添加参数,但我不知道您可以在注释中找到什么等价物...
在config/services.yaml
中:
# filters
app.my_filter:
parent: 'api_platform.doctrine.orm.search_filter'
tags: ['api_platform.filter']
autowire: false
autoconfigure: false
public: false
在resources/customer.yaml
中:
App\Entity\Customer:
collectionOperations:
get:
normalization_context:
groups: ['Customer:Get']
filters: ['app.my_filter']
我打算做的是与 src/Entity/Customer.php
中的注释类似的内容:
/**
* @ApiResource
*
* [...]
*
* @ApiFilter(SearchFilter::class, properties={
* "firstname": "partial",
* "lastname": "partial",
* "emailAddress": "partial",
* "company": "partial"
* })
*/
class Customer
{
...
}
希望您能提供帮助,并清楚地解释我的问题。如果没有,请告诉我,我会尽力为您提供更多详细信息。
谢谢!
我找到了解决问题的方法。看来 API 平台文档毕竟离答案不远了!在资源配置文件中尝试了一段时间后,最终还是决定给services.yaml
文件一个机会。我得到了很大的帮助 on this github issue。所以,这就是我所做的。
步骤 1 - 在 services.yaml
:
# filters
app.customer_resource.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { 'firstname': 'partial', 'lastname': 'partial', 'emailAddress': 'partial', 'owner.emailAddress': 'exact' } ]
tags: [ { name: 'api_platform.filter', id: 'customer.search_filter' } ]
autowire: false # required, explained below
autoconfigure: false # required, explained below
注0:这里需要autowire
和autoconfigure
参数,否则会报错:
服务 "app.customer_resource.search_filter" 上的 "autowire"/"autoconfigure" 属性无法在设置 "parent" 时从“_defaults”继承。将您的子定义移动到单独的文件或在 [...] 中显式定义此属性(加载到资源“[...]”)。
注1:我注意到这里最重要的元素是"id"元素"tags" 参数。在寻找解决方案时,我发现了 vendor/api-platform/core/src/Swagger/Serializer/DocumentationNormalizer::getFilter()
,这表明您必须提供过滤器 ID 才能使其正常工作。在步骤 2 中自己查看。
步骤 2 - 在你的资源配置文件中(我的在 config/resources/customer.yaml
)
App\Entity\Customer:
collectionOperations:
get:
filters: ['customer.search_filter']
# ...
我刚刚使用(就像我说的)重要 过滤器 ID 为 GET 收集操作添加了过滤器。所以,这解决了我的问题。 但是 如果有人有 better/simpler 解决方案,我会很高兴知道。
Screenshot: What the API looks like when SearchFilter is in place
首先,我想通知你,我已经阅读了关于我今天遇到的问题的文档,我也在 Whosebug 和 Google 上进行了搜索,但没有找到正确的解决方案。
为了练习 Symfony 4 和 API 平台,我正在构建客户关系管理 API。我已经了解了很多关于 API 平台的知识,尽管我可以使用注释来配置资源,但我还是强迫自己使用 YAML 来了解更多信息。
因此,我有一个具有多个属性的客户资源:id、firstname、lastname、emailAddress、company、createdAt、updatedAt ,以及 所有者。我的目标是在此资源(在 YAML 中)上添加 SearchFilter 并将大部分属性添加为此过滤器的属性。
我已经尝试过这里给出的解决方案:https://github.com/api-platform/core/issues/1755。问题是我无法让它工作。在 services.yaml 中,我看到您可以添加参数,但我不知道您可以在注释中找到什么等价物...
在config/services.yaml
中:
# filters
app.my_filter:
parent: 'api_platform.doctrine.orm.search_filter'
tags: ['api_platform.filter']
autowire: false
autoconfigure: false
public: false
在resources/customer.yaml
中:
App\Entity\Customer:
collectionOperations:
get:
normalization_context:
groups: ['Customer:Get']
filters: ['app.my_filter']
我打算做的是与 src/Entity/Customer.php
中的注释类似的内容:
/**
* @ApiResource
*
* [...]
*
* @ApiFilter(SearchFilter::class, properties={
* "firstname": "partial",
* "lastname": "partial",
* "emailAddress": "partial",
* "company": "partial"
* })
*/
class Customer
{
...
}
希望您能提供帮助,并清楚地解释我的问题。如果没有,请告诉我,我会尽力为您提供更多详细信息。
谢谢!
我找到了解决问题的方法。看来 API 平台文档毕竟离答案不远了!在资源配置文件中尝试了一段时间后,最终还是决定给services.yaml
文件一个机会。我得到了很大的帮助 on this github issue。所以,这就是我所做的。
步骤 1 - 在 services.yaml
:
# filters
app.customer_resource.search_filter:
parent: 'api_platform.doctrine.orm.search_filter'
arguments: [ { 'firstname': 'partial', 'lastname': 'partial', 'emailAddress': 'partial', 'owner.emailAddress': 'exact' } ]
tags: [ { name: 'api_platform.filter', id: 'customer.search_filter' } ]
autowire: false # required, explained below
autoconfigure: false # required, explained below
注0:这里需要autowire
和autoconfigure
参数,否则会报错:
服务 "app.customer_resource.search_filter" 上的 "autowire"/"autoconfigure" 属性无法在设置 "parent" 时从“_defaults”继承。将您的子定义移动到单独的文件或在 [...] 中显式定义此属性(加载到资源“[...]”)。
注1:我注意到这里最重要的元素是"id"元素"tags" 参数。在寻找解决方案时,我发现了 vendor/api-platform/core/src/Swagger/Serializer/DocumentationNormalizer::getFilter()
,这表明您必须提供过滤器 ID 才能使其正常工作。在步骤 2 中自己查看。
步骤 2 - 在你的资源配置文件中(我的在 config/resources/customer.yaml
)
App\Entity\Customer:
collectionOperations:
get:
filters: ['customer.search_filter']
# ...
我刚刚使用(就像我说的)重要 过滤器 ID 为 GET 收集操作添加了过滤器。所以,这解决了我的问题。 但是 如果有人有 better/simpler 解决方案,我会很高兴知道。
Screenshot: What the API looks like when SearchFilter is in place