Core: custom GET operation and keep default POST and DELETE

Created on 28 Aug 2018  路  2Comments  路  Source: api-platform/core

Hi, I have question. Is it possible to define a custom controller actions for get collection and get item, but for post and delete item keep default actions without overwriting anything?

This is my definition in Entity class:

/**
 * @ApiResource(
 *     itemOperations={
 *          "get"={
 *              "method"="GET",
 *              "path"="/accounts/{id}",
 *              "controller"="App\Controller\Api\ApiAccountController::getAccountItem"
 *          }
 *     },
 *     collectionOperations={
 *          "get_all_accounts"={
 *              "method"="GET",
 *              "path"="/accounts",
 *              "controller"="App\Controller\Api\ApiAccountController::getAccountsCollection"
 *          }
 *     }
 * )
 * @ORM\Entity(repositoryClass="App\Repository\AccountRepository")
 * @ORM\Table(name="bz_account")

and controller:

class ApiAccountController extends AbstractController
{
    /**
     * @Route(
     *     name="get_accounts_collection",
     *     path="/accounts",
     *     methods={"GET"},
     *     defaults={
     *         "_api_resource_class"=Account::class,
     *         "_api_collection_operation_name"="get_all_accounts"
     *     }
     * )
     * @param AccountRepository $repository
     * @return Account[]
     */
    public function getAccountsCollection(AccountRepository $repository)
    {
        // return ...
    }

    /**
     * @Route(
     *     name="get_account_item",
     *     path="/accounts/{id}",
     *     methods={"GET"},
     *     defaults={
     *         "_api_resource_class"=Account::class,
     *         "_api_item_operation_name"="get_account"
     *     }
     * )
     * @param Account|null $account
     * @return Account
     */
    public function getAccountItem(?Account $account)
    {
        // return ...
    }
}

it works like I wanted to, but I need a post and delete actions which works fine and I don't need custom actions.
When I add post and delete methods to itemOperations:

 *     itemOperations={
 *          "get"={
 *              "method"="GET",
 *              "path"="/accounts/{id}",
 *              "controller"="App\Controller\Api\ApiAccountController::getAccountItem"
 *          },
 *          "post"={"method"="POST"},
 *          "delete"={"method"="DELETE"}
 *     },

I got an error

An exception has been thrown during the rendering of a template ("There is no builtin action for the item POST operation. You need to define the controller yourself in . (which is being imported from "C:\Users\...\config\routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.").

how can I make it work?

Most helpful comment

@norkunas yes now I see it. Post should go to collection operations. Thanks

All 2 comments

Your item operation name should be put not post and method also PUT.

@norkunas yes now I see it. Post should go to collection operations. Thanks

Was this page helpful?
0 / 5 - 0 ratings