【问题标题】:custom operation working when declared as collection but not working when declared as item自定义操作在声明为集合时有效,但在声明为项目时无效
【发布时间】:2018-01-20 13:27:49
【问题描述】:

我正在尝试创建自定义操作。它应该为给定的slug 返回一个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()
 */

它总是返回这个错误:

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()
 */

那么,有什么问题吗??

谢谢,

【问题讨论】:

    标签: api symfony swagger api-platform.com


    【解决方案1】:

    对于自定义项目操作,{id} 是强制性的,并且它必须在路由中。但就我而言,我没有使用它。我正在使用{slug}。这就是我收到错误消息的原因。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-19
      • 2014-07-20
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多