带有 API 平台包的自定义 Symfony 操作 [英] Custom Symfony Action with API Platform bundle
问题描述
我尝试使用 Symfony 包 API-Platform 构建 API.
I try to build an API with the Symfony bundle API-Platform.
API 资源通过 HTTP 动词 POST、GET、PUT、DELETE 提供自动 CRUD 操作.
Api resource offer automatic CRUD action with the HTTP verbs POST, GET, PUT, DELETE.
我想要的是添加一个端点来处理自定义 POST 操作,具有自定义负载/正文,不依赖于任何资源.
What I want is adding an endpoint to handle a custom POST action, with a custom payload/body, not depending on any resource.
我阻止它的地方是将此端点添加到自动 API 平台文档中.
Where I block it is to add this endpoint to the automatic API-Platform documentation.
在GitHub上找这种问题的时候发现API-Platform v2应该可以做到.
When looking for this kind of issue on GitHub, I found that the API-Platform v2 should be able to do it.
See Issue #385 : Custom action + ApiDoc
好像有人找到了使用 NelmioApiDoc @ApiDoc 注释的方法.
It looks like some people find the way to use NelmioApiDoc @ApiDoc annotation.
See Issue #17 : Documentation for custom operation
推荐答案
使用 @ApiDoc
注释是行不通的,API Platform 3 中将取消对 NelmioApiDoc 的支持,转而使用内置的 Swagger/Hydra 支持.
Using the @ApiDoc
annotation is a no go, support for NelmioApiDoc will be removed in API Platform 3 in favor of the builtin Swagger/Hydra support.
如果您使用自定义 API 平台操作,则应自动记录该操作在 Swagger 和 Hydra 文档中.
If you use a custom API Platform action, the action should automatically be documented in Swagger and Hydra docs.
无论如何,您始终可以自定义 Swagger(和 Hydra)文档以添加自定义端点或其他任何内容:https://github.com/api-platform/docs/blob/master/core/swagger.md#override-swagger-documentation(该文档很快就会在网站上提供).
Anyway, you can always customize the Swagger (and Hydra) docs to add custom endpoints or anything else: https://github.com/api-platform/docs/blob/master/core/swagger.md#override-swagger-documentation (this documentation will be available on the website soon).
这篇关于带有 API 平台包的自定义 Symfony 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!