简体   繁体   English

Symfony 和 API 平台 - 通过 slug 或 userToken 或任何其他不同于 ID 的字段检索数据

[英]Symfony and API Platform - retrieving data by slug or userToken or any other field different than ID

What I'm trying to do is:我想要做的是:

  • send a request from my frontend to API custom url to activate user account (/account/activate/someTokenValue)从我的前端向 API 自定义 url 发送请求以激活用户帐户 (/account/activate/someTokenValue)
  • retrieve user form database by confirmationToken value通过confirmationToken值检索用户表单数据库
  • activate user account and save it to database激活用户帐户并将其保存到数据库
  • return activation information (or some errors if eg token is invalid)返回激活信息(或一些错误,例如令牌无效)

So I've defined:所以我定义了:

* @ApiResource(itemOperations={
 *     "get",
 *     "activate_account"={
 *         "method"="get",
 *         "path"="/account/activate/{confirmationToken}",
 *         "controller"=UserActivate::class
 *     }
 * })
 */

In my User entity class there is UserActivate controller and UserActivateHandler that is invoked by UserActivate controller.在我的User实体类中,有UserActivate控制器和UserActivateHandlerUserActivate控制器调用。 I've set ApiProperty identifier for ID to false and for confirmationToken to true .我给自己定ApiProperty identifier的ID,以falseconfirmationTokentrue

   /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @ApiProperty(identifier=false)
     *
     */
    private $id;

    /**
      * 
      * @ORM\Column(type="string", nullable=true)
      * @ApiProperty(identifier=true)
      */
    protected $confirmationToken;

However API Platform still requires an ID and it doesn't seem to see confirmationToken parameter.但是 API 平台仍然需要一个 ID,它似乎没有看到confirmationToken参数。

Basically my question is, how I can retrieve an object by, in this case, confirmationToken ?基本上我的问题是,我怎么能检索由一个对象,在这种情况下, confirmationToken

I had the same problem, getting an entity by a different property than the one indicated as the identifier;我遇到了同样的问题,通过与标识符指示的属性不同的属性获取实体; you did well with the property annotations:您在属性注释方面做得很好:

@ApiProperty(identifier=false) 
@ApiProperty(identifier=true)

but at the same time you have to mantain the {id} in your path, so change但同时你必须在你的路径中保持 {id},所以改变

"path"="/account/activate/{confirmationToken}",

to

"path"="/account/activate/{id}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM