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

Jak*_*kub 5 php api doctrine symfony api-platform.com

我想做的是:

  • 从我的前端向 API 自定义 url 发送请求以激活用户帐户 (/account/activate/someTokenValue)
  • 通过confirmationToken值检索用户表单数据库
  • 激活用户帐户并将其保存到数据库
  • 返回激活信息(或一些错误,例如令牌无效)

所以我定义了:

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

在我的User实体类中有一个UserActivate控制器,并由控制器UserActivateHandler调用UserActivate。我已将ApiProperty identifierID设置false为 to 和confirmationTokento true

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

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

但是API Platform仍然需要ID并且它似乎没有看到confirmationToken参数。

基本上我的问题是,在这种情况下,我如何检索对象confirmationToken

小智 1

我遇到了同样的问题,通过与标识符指示的属性不同的属性获取实体;您在属性注释方面做得很好:

@ApiProperty(identifier=false) 
@ApiProperty(identifier=true)
Run Code Online (Sandbox Code Playgroud)

但同时你必须在你的路径中保留 {id} ,所以改变

"path"="/account/activate/{confirmationToken}",
Run Code Online (Sandbox Code Playgroud)

"path"="/account/activate/{id}"
Run Code Online (Sandbox Code Playgroud)