使用 API 平台包的自定义 Symfony 操作
Custom Symfony Action with API Platform bundle
我尝试使用 Symfony 包 API-Platform 构建一个 API。
Api 资源提供使用 HTTP 动词 POST、GET、PUT、DELETE 的自动 CRUD 操作。
我想要的是添加一个端点来处理自定义 POST 操作,使用自定义 payload/body,不依赖于任何资源。
我阻止它的地方是将此端点添加到自动 API-平台文档中。
在GitHub上查找此类问题时,发现API-Platform v2应该可以做到。
见Issue #385 : Custom action + ApiDoc
看来有些人找到了使用 NelmioApiDoc @ApiDoc 注释的方法。
使用 @ApiDoc
注释是不行的,API 平台 3 将删除对 NelmioApiDoc 的支持,取而代之的是内置 Swagger/Hydra 支持。
如果您使用 custom API Platform action,该操作应自动记录在 Swagger 和 Hydra 文档中。
无论如何,您始终可以自定义 Swagger(和 Hydra)文档以添加自定义端点或任何其他内容:https://github.com/api-platform/docs/blob/master/core/swagger.md#override-swagger-documentation(该文档很快就会在网站上提供)。
您可以使用 @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
我运行陷入同样的情况,因为我试图将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}",
* }
* })
希望对偶然发现这个问题的其他人有所帮助。
这里是来自the great documentation about it的段落:
Collection operations act on a collection of resources. By default two
routes are implemented: POST and GET. Item operations act on an
individual resource. 3 default routes are defined GET, PUT and 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中添加public属性
class User {
public $login
public $password
}
一切就绪,现在大摇大摆 ui 你会看到带有登录和传递参数的方法 POST /api/auth。
在你的控制器中覆盖 _invoke 以执行你的逻辑。
class AuthController {
public function __invoke()
{
return ['your custom answer'];
}
}
我尝试使用 Symfony 包 API-Platform 构建一个 API。
Api 资源提供使用 HTTP 动词 POST、GET、PUT、DELETE 的自动 CRUD 操作。
我想要的是添加一个端点来处理自定义 POST 操作,使用自定义 payload/body,不依赖于任何资源。
我阻止它的地方是将此端点添加到自动 API-平台文档中。
在GitHub上查找此类问题时,发现API-Platform v2应该可以做到。
见Issue #385 : Custom action + ApiDoc
看来有些人找到了使用 NelmioApiDoc @ApiDoc 注释的方法。
使用 @ApiDoc
注释是不行的,API 平台 3 将删除对 NelmioApiDoc 的支持,取而代之的是内置 Swagger/Hydra 支持。
如果您使用 custom API Platform action,该操作应自动记录在 Swagger 和 Hydra 文档中。
无论如何,您始终可以自定义 Swagger(和 Hydra)文档以添加自定义端点或任何其他内容:https://github.com/api-platform/docs/blob/master/core/swagger.md#override-swagger-documentation(该文档很快就会在网站上提供)。
您可以使用 @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
我运行陷入同样的情况,因为我试图将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}",
* }
* })
希望对偶然发现这个问题的其他人有所帮助。
这里是来自the great documentation about it的段落:
Collection operations act on a collection of resources. By default two routes are implemented: POST and GET. Item operations act on an individual resource. 3 default routes are defined GET, PUT and 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中添加public属性
class User {
public $login
public $password
}
一切就绪,现在大摇大摆 ui 你会看到带有登录和传递参数的方法 POST /api/auth。
在你的控制器中覆盖 _invoke 以执行你的逻辑。
class AuthController {
public function __invoke()
{
return ['your custom answer'];
}
}