我正在尝试在Symfony中对密码进行编码,在仔细阅读了此处的文档之后,看来我还是在做错事情。
这是我的RegisterController.php:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\Type\UserType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class RegisterController extends AbstractController
{
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
/**
* @Route("/register", name="user.register")
*/
public function create(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$this->passwordEncoder->encodePassword( $user, $user->getPassword() )
);
上面返回以下错误:
属性“ plainPassword”或方法“ getPlainPassword()”,“ plainPassword()”,“ isPlainPassword()”,“ hasPlainPassword()”,“ __ get()”都不存在,并且在类“ App \实体\用户”。
这是我的Register.twig.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Register{% endblock %}</title>
{# {% block stylesheets %}{% endblock %} #}
</head>
<body>
<div class="container">
<h2 class="form-signin-heading">Welcome, please register below</h2>
{{ form(form) }}
</div>
</body>
</html>
最后,我在security.yaml
文件中有了以下设置:
security:
encoders:
App\Entity\User:
algorithm: auto
我想这很简单,但是我无法使它起作用。这是我第一次来Symfony
。
所以,我发现了错误a!我已经plainPassword
在UserType.php中设置了该字段:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('email', EmailType::class)
->add('birthdate', DateType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
))
->add('save', SubmitType::class, ['label' => 'Register']);
}
...
将此更改为password
我在User
实体中的状态可以更正错误。
谢谢大家的帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句