webstack/api-platform-extensions-bundle

扩展包,用于添加uuid过滤器,或为Doctrine ORM添加API Platform的全局搜索支持

v3.1.2 2023-03-07 09:28 UTC

This package is auto-updated.

Last update: 2024-09-03 16:12:15 UTC


README

TODO:API Platform是什么?我们如何扩展它?

安装

composer require webstack/api-platform-extensions-bundle:[版本号]

配置

安装插件后,您需要将一些配置文件添加到项目中

  • config/packages/webstack_api_platform_extensions.yaml
webstack_api_platform_extensions:
  identifier_class: App\Entity\%UserEntity%
  • config/routes/api_platform.yaml
app_extra:
  resource: '@WebstackApiPlatformExtensionsBundle/Resources/config/routing/routing.xml'
  prefix: /api

过滤器

此扩展包添加了以下过滤器

全局搜索过滤器(api_platform.doctrine.orm.global_search_filter

递归地搜索指定列(或所有列,有些吓人)的实体,该过滤器在其激活的端点上激活。

将过滤器作为服务配置(config/services/api_platform/search/[$domain/]$entity.yaml

services:
  api.resource.region.global.search_filter:
    parent: 'api_platform.doctrine.orm.global_search_filter'
    arguments: [ {
      'name': 'partial',
      'depot.description': 'partial',
    } ]
    tags: [ { name: 'api_platform.filter', id: 'api.region.global_search_filter' } ]
    autowire: false
    autoconfigure: false

并将其应用于适当的资源

App\Entity\Transport\Region:
  attributes:
    route_prefix: /transport
    filters:
      - 'api.region.global_search_filter'

现在当用户使用?_global_search=foo调用API时,查询将类似于以下内容

SELECT 
    r.*
FROM 
    region r
LEFT JOIN
    depot d
ON
    d.id = r.depot_id
WHERE
    r.name LIKE '%foo%' 
    OR d.description LIKE '%foo%'

别名搜索过滤器(api_platform.doctrine.orm.alias_search_filter

与常规搜索过滤器类似,但允许您在URL中重命名或别名(嵌套)属性。传递属性和别名作为独立的DI参数

services:
  api.some_entity.deeply_nested_prop_filter:
    parent: 'api_platform.doctrine.orm.alias_search_filter'
    arguments:
      $properties:
        deeply.nested.property: 'exact'
      $aliases:
        myProp: 'deeply.nested.property'
    tags: [ { name: 'api_platform.filter' } ]

并将其应用于适当的资源

App\Entity\SomeEntity:
  attributes:
    route_prefix: /some
    filters:
      - 'api.some_entity.deeply_nested_prop_filter'

现在要使用默认的SearchFilter访问此过滤器,URL将是

/some?deeply.nested.property=foo

此别名搜索过滤器添加了一个新的查询字符串参数,并将其映射到配置的别名

/some?myProp=foo

或搜索过滤器(api_platform.doctrine.orm.or_search_filter

TODO

UUID过滤器(api_platform.doctrine.orm.uuid_filter

用于通过UUID查找嵌套实体,因为API Platform不支持这一点(请参阅api-platform/core#3774,由于它破坏了日期搜索而被撤销)。

服务配置

services:
  api.resource.transport_position.vehicle_filter:
    parent: 'api_platform.doctrine.orm.uuid_filter'
    arguments: [ {
      vehicle.id: 'exact'
    } ]
    tags: [ { name: 'api_platform.filter', id: 'api.transport_position.vehicle_filter' } ]
    autowire: false
    autoconfigure: false
    public: false

API Platform实体配置

App\Entity\Transport\Position:
  collectionOperations:
    get:
      filters:
        - 'api.transport_position.vehicle_filter'

现在API调用者可以使用GET .../transport_positions?vehicle.id=$uuid进行过滤。

路由

此扩展包引入了以下路由

/me

获取调用者的信息。包括SwaggerDecorator以生成此端点的OpenAPI文档。