自定义操作在声明为集合时有效但在声明为项目时无效

custom operation working when declared as collection but not working when declared as item

我正在尝试创建自定义操作。对于给定的 slug,它应该 return Product 资源。 为此,

声明为项目的自定义操作

api_product_slug:
    path: '/api/products/by-slug/{slug}'
    methods:  ['GET']
    defaults:
        _controller: 'App\Controller\ProductController:productsGetBySlugAction'
        _api_resource_class: 'App\Entity\Product'
        _api_item_operation_name: 'product_slug'

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 * @ApiResource(attributes={"pagination_client_items_per_page"=true,
 *                          "filters"={"product.search"}
 *                         },
 *              collectionOperations={
 *                      "get"={
 *                          "method"="GET",
 *                          "normalization_context"={"groups"={"product_gets"}} },
 *                      "post"={
 *                          "method"="POST",
 *                          "denormalization_context"={"groups"={"product_post"}} }

 *              },
 *              itemOperations={
 *                      "get"={
 *                          "method"="GET",
 *                          "normalization_context"={"groups"={"product_get"}} },
 *                      "put"={
 *                          "method"="PUT",
 *                          "denormalization_context"={"groups"={"product_put"}} },
 *                      "delete"={
 *                          "method"="DELETE"},
 *                      "product_slug"={
 *                          "route_name"="api_product_slug",
 *                          "swagger_context" = {
 *                              "parameters" = {
 *                                  {
 *                                  "name" = "slug",
 *                                  "in" = "path",
 *                                  "required" = "true",
 *                                  "type" = "string"
 *                                  }
 *                              },
 *                              "responses" = {
 *                                  "200"={
 *                                      "description"="Product found"
 *                                  },
 *                                  "404"={
 *                                      "description"="Product not found"
 *                                  }
 *                              },
 *                              "summary"="Returns a Product resource for a given slug",
 *                          }
 *                      }
 *              })
 * @ORM\HasLifecycleCallbacks()
 */

总是return出现这个错误:

Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
Not Found

  at vendor/api-platform/core/src/EventListener/ReadListener.php:112
  at ApiPlatform\Core\EventListener\ReadListener->getItemData(object(Request), array('resource_class' => 'App\Entity\Product', 'item_operation_name' => 'product_slug', 'receive' => true))
     (vendor/api-platform/core/src/EventListener/ReadListener.php:64)
  at ApiPlatform\Core\EventListener\ReadListener->onKernelRequest(object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
  at call_user_func(array(object(ReadListener), 'onKernelRequest'), object(GetResponseEvent), 'kernel.request', object(TraceableEventDispatcher))
     (vendor/symfony/event-dispatcher/Debug/WrappedListener.php:104)
  at Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(object(GetResponseEvent), 'kernel.request', object(EventDispatcher))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:212)
  at Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(array(object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener), object(WrappedListener)), 'kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/EventDispatcher.php:44)
  at Symfony\Component\EventDispatcher\EventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/event-dispatcher/Debug/TraceableEventDispatcher.php:139)
  at Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.request', object(GetResponseEvent))
     (vendor/symfony/http-kernel/HttpKernel.php:125)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:190)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:34)

但是,如果我将它声明为一个集合,它就可以完美运行。

声明为集合的自定义操作

api_product_slug:
    path: '/api/products/by-slug/{slug}'
    methods:  ['GET']
    defaults:
        _controller: 'App\Controller\ProductController:productsGetBySlugAction'
        _api_resource_class: 'App\Entity\Product'
        _api_collection_operation_name: 'product_slug'

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 * @ApiResource(attributes={"pagination_client_items_per_page"=true,
 *                          "filters"={"product.search"}
 *                         },
 *              collectionOperations={
 *                      "get"={
 *                          "method"="GET",
 *                          "normalization_context"={"groups"={"product_gets"}} },
 *                      "post"={
 *                          "method"="POST",
 *                          "denormalization_context"={"groups"={"product_post"}} },
 *                      "product_slug"={
 *                          "route_name"="api_product_slug",
 *                          "swagger_context" = {
 *                              "parameters" = {
 *                                  {
 *                                  "name" = "slug",
 *                                  "in" = "path",
 *                                  "required" = "true",
 *                                  "type" = "string"
 *                                  }
 *                              },
 *                              "responses" = {
 *                                  "200"={
 *                                      "description"="Product found"
 *                                  },
 *                                  "404"={
 *                                      "description"="Product not found"
 *                                  }
 *                              },
 *                              "summary"="Returns a Product resource for a given slug",
 *                          }
 *                       }
 *              },
 *              itemOperations={
 *                      "get"={
 *                          "method"="GET",
 *                          "normalization_context"={"groups"={"product_get"}} },
 *                      "put"={
 *                          "method"="PUT",
 *                          "denormalization_context"={"groups"={"product_put"}} },
 *                      "delete"={
 *                          "method"="DELETE"}
 *              })
 * @ORM\HasLifecycleCallbacks()
 */

那么,怎么了??

谢谢,

对于自定义物品的操作,{id}是必须的,必须在路线中。但就我而言,我没有使用它。我正在使用 {slug}。这就是我收到错误的原因。

请参考https://github.com/api-platform/api-platform/issues/217