创建自定义操作(collectionOperations)和控制器

时间:2018-02-21 07:16:47

标签: api-platform.com

文档中有一个示例操作(https://api-platform.com/docs/core/operations#creating-custom-operations-and-controllers)。如何为收集操作做同样的事情?这就是做了什么,但它不起作用。

    // src/AppBundle/Entity/Book.php

    * @ApiResource(collectionOperations={
    *     "get",
    *     "special"={"route_name"="book_special"}
    * })

    class Book
    {
       ...
    }

路由:

 book_special: 
 path: '/books/special'
 methods:  ['POST']
 defaults:
     _controller: 'AppBundle:Book:special'
     _api_resource_class: 'AppBundle\Entity\Book'
     _api_collection_operation_name: 'special'

和控制器:

class BookController extends Controller
{
    public function specialAction(Book $book, Request $request)
    {
        //in the variable $book there is nothing
        //in the variable $request there is nothing
        return $book;
    }
}

如何在控制器中接收数据api请求?

1 个答案:

答案 0 :(得分:1)

正如文档中所述:“按惯例,必须将动作的参数称为$data。”。

以下应该可以解决问题:

public function specialAction(Book $data, Request $request)
{
   // ...
}

这是因为API平台内部使​​用Symfony's request attributes