如何在Symfony中为任意实体的密码编码?

我正在mysql中创建组织表。

该实体称为Organisation,它具有一个password字段。

当我尝试使用UserPasswordEncoderInterface时,它期望使用user实体,所以这不起作用。我尝试使用PasswordEncoderInterface,但是它说该服务不存在。在这里可以做什么?

这是我的代码:

   public function register(Request $request,EntityManagerInterface $entityManager,PasswordEncoderInterface $encoder)
    {
        $organisation = new Organisation();

        $form = $this->createForm(OrganisationRegisterType::class,$organisation);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $plainPassword = $form->getData()->getPassword();
            $encoded = $encoder->encodePassword($plainPassword,null);
            $organisation->setPassword($encoded);

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($organisation);
            $entityManager->flush();

            $this->addflash(
                'success','Organizacija sukurta sėkmingai. Nurodytu el. paštu buvo išsiųsta prisijungimo informacija'
            );
        }

这是我得到的错误:

  

无法自动装配“ App \ Controller \ OrganisationController :: register()”的参数$ encoder:它引用接口“ Symfony \ Component \ Security \ Core \ Encoder \ PasswordEncoderInterface”,但是不存在这样的服务。您是否创建了实现此接口的类?

realqishangbaxia 回答:如何在Symfony中为任意实体的密码编码?

PasswordEncoderInterface不存在。

最简单的解决方案是使您的Organisation类实现UserInterface

UserPasswordEncoder不需要User对象,但是可以实现该接口的对象。即使您的组织类本身不是“用户”,您似乎也希望它具有相同的界面(用户名,密码...)。

只需更改您的组织类以实现该接口,然后照常注入UserPasswordEncoderInterface

,

如果@yivi的建议不适用于您的实体类,则可以将相应的编码器实现手动注入到控制器中。要么访问控制器中的容器,要么更好的访问configure your controllers as services并通过setter injection注入服务。

本文链接:https://www.f2er.com/3152610.html

大家都在问