Php 带有API平台捆绑包的自定义Symfony操作
我尝试用Symfony包构建一个API Api资源提供了带有HTTP谓词POST、GET、PUT和DELETE的自动CRUD操作 我想要的是添加一个端点来处理一个定制的POST操作,使用一个定制的负载/主体,而不依赖于任何资源 我阻止它的地方是将该端点添加到自动API平台文档中 在GitHub上查找此类问题时,我发现API平台v2应该能够做到这一点 看 看起来有些人找到了使用NelmioApiDoc@ApiDoc注释的方法Php 带有API平台捆绑包的自定义Symfony操作,php,symfony,api-platform.com,Php,Symfony,Api Platform.com,我尝试用Symfony包构建一个API Api资源提供了带有HTTP谓词POST、GET、PUT和DELETE的自动CRUD操作 我想要的是添加一个端点来处理一个定制的POST操作,使用一个定制的负载/主体,而不依赖于任何资源 我阻止它的地方是将该端点添加到自动API平台文档中 在GitHub上查找此类问题时,我发现API平台v2应该能够做到这一点 看 看起来有些人找到了使用NelmioApiDoc@ApiDoc注释的方法 请参见使用@ApiDoc注释是不可能的,API平台3将删除对Nelmio
请参见使用
@ApiDoc
注释是不可能的,API平台3将删除对NelmioApiDoc的支持,以支持内置的招摇过市/Hydra
如果您使用a,则该操作应自动记录在Swagger和Hydra文档中
无论如何,您始终可以自定义Swagger(和Hydra)文档,以添加自定义端点或其他任何内容:(此文档将很快在网站上提供)。您可以使用
@apirource()
注释记录自己的路线:
/**
* @ORM\Entity
* @ApiResource(
* itemOperations={
* "get"={"method"="GET"},
* "put"={"method"="PUT"},
* "delete"={"method"="DELETE"},
* "send_reset_password_token"={
* "route_name"="user_send_reset_password_token",
* "swagger_context" = {
* "parameters" = {
* {
* "name" = "email",
* "in" = "path",
* "required" = "true",
* "type" = "string"
* }
* },
* "responses" = {
* "201" = {
* "description" = "email with reset token has been sent",
* "schema" = {
* "type" = "object",
* "required" = {
* "email"
* },
* "properties" = {
* "email" = {
* "type" = "string"
* },
* "fullname" = {
* "type" = "string"
* }
* }
* }
* },
* "400" = {
* "description" = "Invalid input"
* },
* "404" = {
* "description" = "resource not found"
* }
* },
* "summary" = "Send email with token to reset password",
* "consumes" = {
* "application/json",
* "text/html",
* },
* "produces" = {
* "application/json"
* }
* }
* }
* },
* attributes={
* "normalization_context"={"groups"={"user", "user-read"}},
* "denormalization_context"={"groups"={"user", "user-write"}}
* }
* )
*/
来源:我遇到了同样的情况,因为我试图将POST方法放入
itemOperations
,尽管它只能驻留在collectionOperations
中。在后者中,可以成功地定义我的自定义路径
/**
* @ApiResource(
* collectionOperations={
* "get"={
* "path"="/name_your_route",
* },
* "post"={
* "path"="/name_your_route",
* },
* },
* itemOperations={
* "get"={
* "path"="/name_your_route/group/{groupId}/user/{userId}",
* "requirements"={"groupId"="\d+", "userId"="\d+"},
* },
* "delete"={
* "path"="/name_your_route/group/{groupId}/user/{userId}",
* },
* "put"={
* "path"="/name_your_route/group/{groupId}/user/{userId}",
* }
* })
希望对其他偶然发现这个问题的人有所帮助
以下是以下段落:
集合操作作用于资源集合。默认情况下是两个
实现了以下路径:POST和GET。项目操作作用于
个人资源。定义了3个默认路由:GET、PUT和DELETE
您可以像这样创建自定义post操作。 将资源配置映射到yaml
# config/packages/api_platform.yaml
api_platform:
enable_swagger_ui: false
mapping:
paths: ['%kernel.project_dir%/config/api_platform']
创建resources.yaml
# config/api_platform/resources.yaml
resources:
App\Entity\User:
itemOperations: []
collectionOperations:
post:
method: 'POST'
path: '/auth'
controller: App\Controller\AuthController
swagger_context:
summary: your desc
description: your desc
然后在App\Entity\User中添加公共属性
class User {
public $login
public $password
}
现在,在swagger ui中,您将看到带有登录和传递参数的方法POST/api/auth
In-u控制器覆盖_调用以执行逻辑
class AuthController {
public function __invoke()
{
return ['your custom answer'];
}
}
中描述的自定义操作不允许自定义车身/有效负载。操作仍然会获得一个
$data
参数,该参数是为POST或PUT方法创建或更新的实体/资源。感谢您提供的Swagger链接,我会尽快查看。覆盖Swagger只允许为每个自定义操作手动添加每个自定义参数。它不像Nelmio那样,为参数或返回类型提供自动文档。我不知道这是否是API平台的目标。但是,随着API平台的发展,并被Symfony Flex提出为最佳的API解决方案,我发现很难管理简单的自定义操作NelmioApiDoc的功能目前看来是一个很好的解决方案:它是干净的,允许为内置UI保留API平台文档,并使用所有NelmioApiDoc功能进行自定义操作。@KévinDunglas对于这个问题您有什么更新或新的建议吗?谢谢