尝试使用symfony 5和API平台为我的API添加自定义操作时出现错误

问题描述 投票:-1回答:1

我想在我的API平台中添加自定义操作,并添加有关此API的文档。我有一个Mobile实体,它与我的User实体一对一。

我想按用户插入移动令牌,并且如果用户已经拥有一个移动令牌,我只想替换当前令牌而不添加新的令牌。

在我的移动实体中,我为自定义操作添加了注释:

/**
 * @ApiResource(
 *     normalizationContext={"groups"="mobile_read"},
 *     collectionOperations={
 *        "GET",
 *        "insert"= {
 *          "method" = "post",
 *          "controller" = "App\Controller\OperationsToken",
 *          "path" = "/mobiles/insert",
 *          "swagger_context" = {
 *                "summary" = "insert or replace mobile token",
 *           }
 *        }
 *     },
 *     itemOperations={}
 * )
 * @ApiFilter(SearchFilter::class, properties={"owner": "exact", "state":"exact"})
 * @ApiFilter(OrderFilter::class, properties={"id"}, arguments={"orderParameterName"="order"})
 * @ORM\Entity(repositoryClass="App\Repository\GSMRepository")
 */
class Mobile

并且我创建了OperationToken Controller来添加链接到我的API资源的操作:

控制器实现了invoke方法:

public function __invoke(Mobile $data)
    {
        $user = $this->security->getUser();
        $token = $data->getToken();
        if ($user->getMobile()) {
            $mobile = $user->getMobile();
            $mobile->setToken($token);
            $this->manager->flush();
        } else {
            $mobile = new Mobile();
            $mobile->setToken($token);
            dd($mobile);
            $mobile->setOwner($user);
            $this->manager->flush();
        }
    }

我试图用Postman测试自定义操作。

我有错误:

error

symfony api-platform.com
1个回答
0
投票

尝试添加“ read” = false,

 *     collectionOperations={
 *        "GET",
 *        "insert"= {
 *          "method" = "post",
 *          "controller" = "App\Controller\OperationsToken",
 *          "path" = "/mobiles/insert",
 *          "read"=false,
© www.soinside.com 2019 - 2024. All rights reserved.