1
votes

The documentation has an example for the item Operation (https://api-platform.com/docs/core/operations#creating-custom-operations-and-controllers). How to do the same for collection operations? That's what's done, but it does not work.

    // src/AppBundle/Entity/Book.php

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

    class Book
    {
       ...
    }

routing:

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

And Controller:

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;
    }
}

How in the controller to receive data api request?

1

1 Answers

1
votes

As stated in the docs: "By convention, the the action's parameter must be called $data.".

The following should do the trick:

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

This is because API Platform internally uses Symfony's request attributes.