4

我尝试使用 Symfony 捆绑包API-Platform构建 API 。

Api 资源通过 HTTP 动词 POST、GET、PUT、DELETE 提供自动 CRUD 操作。

我想要的是添加一个端点来处理自定义 POST 操作,使用自定义有效负载/正文,而不依赖于任何资源。

我阻止它的地方是将此端点添加到自动 API 平台文档中。

在 GitHub 上查找此类问题时,发现 API-Platform v2 应该可以做到。

请参阅问题 #385:自定义操作 + ApiDoc

看起来有些人找到了使用 NelmioApiDoc @ApiDoc 注释的方法。

请参阅问题 #17:自定义操作的文档

4

4 回答 4

5

使用@ApiDoc注释是不行的,对 NelmioApiDoc 的支持将在 API 平台 3 中删除,转而支持内置的 Swagger/Hydra。

如果您使用自定义 API 平台操作,则该操作应自动记录在 Swagger 和 Hydra 文档中。

无论如何,您始终可以自定义 Swagger(和 Hydra)文档以添加自定义端点或其他任何内容:https ://github.com/api-platform/docs/blob/master/core/swagger.md#override-swagger-documentation (此文档将很快在网站上提供)。

于 2017-08-23T12:55:01.580 回答
3

@ApiResource()您可以使用注释记录自己的路线:

/**
 * @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"}}
 *     }
 * )
 */

来源:https ://github.com/api-platform/docs/issues/143#issuecomment-260221717

于 2018-03-28T12:31:53.590 回答
1

您可以像这样创建自定义发布操作。将资源配置映射到 yaml。

# config/packages/api_platform.yaml
api_platform:
enable_swagger_ui: false
mapping:
    paths: ['%kernel.project_dir%/config/api_platform']

创建资源.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。

在 u 控制器中覆盖 _invoke 以执行您的逻辑。

class AuthController {
   public function __invoke()
   {
      return ['your custom answer'];
   }
}
于 2018-12-02T20:09:44.320 回答
0

我遇到了同样的情况,因为我试图将 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

于 2018-07-26T03:01:19.980 回答