Php 如何在Symfony中对任意实体的密码进行编码?
我正在mysql中创建organizations表 该实体称为Php 如何在Symfony中对任意实体的密码进行编码?,php,symfony,symfony4,symfony-security,Php,Symfony,Symfony4,Symfony Security,我正在mysql中创建organizations表 该实体称为组织,它有一个密码字段 当我尝试使用UserPasswordEncoderInterface时,它需要user实体,因此这不起作用。我尝试使用PasswordEncoderInterface,但它说该服务不存在。这里可以做什么 这是我的代码: public function register(Request $request, EntityManagerInterface $entityManager, PasswordEnco
组织
,它有一个密码
字段
当我尝试使用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\organizationcontroller::register()”的参数$encoder:它引用接口“Symfony\Component\Security\Core\encoder\PasswordEncoderInterface”,但不存在此类服务。您是否创建了实现此接口的类`
密码EncoderInterface
不存在
最简单的解决方案是让您的组织
类实现用户界面
UserPasswordEncoder
不需要User
对象,而是实现该接口的对象。即使您的组织类本身不是“用户”,看起来您希望它具有相同的接口(用户名、密码…)
只需更改您的组织类以实现该接口,并像往常一样插入
用户密码Encoderface
。密码Encoderface不存在
最简单的解决方案是让您的组织
类实现用户界面
UserPasswordEncoder
不需要User
对象,而是实现该接口的对象。即使您的组织类本身不是“用户”,看起来您希望它具有相同的接口(用户名、密码…)
只需更改您的组织类以实现该接口,并像往常一样注入
UserPasswordEncoderInterface
。如果@yivi的建议不适用于您的实体类,您可以手动将相应的编码器实现注入控制器。访问控制器中的容器,或者更好地通过注入服务。如果@yivi的建议不适用于您的实体类,您可以手动将相应的编码器实现注入控制器。访问控制器中的容器,或者更好地通过注入服务