1

我正在寻找方法来找出检索资源,而不是取决于它的 id 而是类似username, GET /api/user/{username}.

所以我做了这样的事情:

/**
* @ApiRessource({
*     itemOperations={
*         "get"={
*             "normalization_context"={"groups"={"user:read"}},
*             "path"="/user/{username}", "read"=false,
*             "controller"=ApiUser::class,
*             "openapi_context"={
*                  "summary" = "Récupérer un freehelper en particulier par son nom"
*              },
*          },
})
*/
class User{
{
    /**
     * @Groups({"user:read"})
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @ApiProperty(identifier=false)
     */
    private $id;

    /**
     * @Groups({"user:read"})
     * @ORM\Column(type="string", length=56, unique=true)
     * @ApiProperty(identifier=true)
     */
    private $username;
....
}

然后我按照这里dataproider的描述写了,所以我收到了这个错误

"detail": "Unable to generate an IRI for \"App\\Entity\\User\".",

你能帮我理解怎么做吗?

4

1 回答 1

0

You can create a function in ApiUser controller like that:

/**
 * @Route("/user/{username}", name="api_user_username", methods={"GET"})
 */
public function getByUsername($username)
{
    $event = $this->em->getRepository(User::class)
                ->findOneBy(["username" => $username));

    return $this->httpKernel
                ->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
}
 
于 2020-08-02T19:38:05.700 回答