我正在尝试创建自定义操作。它应该返回给定Product
的{{1}}资源。
为此,
自定义操作声明为项目
slug
和
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'
那么,出了什么问题?
谢谢,
答案 0 :(得分:0)
对于自定义项目操作,{id}
是强制性的,必须在路径中。但就我而言,我并没有使用它。我正在使用{slug}
。这就是我收到错误的原因。