当我尝试在 symfony 4 中散列密码时出现的错误是什么
What is this error I get when I try to hash my password in symfony 4
这是错误
Argument 1 passed to
Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword()
must be an instance of
Symfony\Component\Security\Core\User\UserInterface, instance of
App\Entity\User given, called in
C:\Users\willi\Desktop\Crowdin\src\Controller\CompteController.php on
line 44
CompteController.php :
/**
* @Route("/inscriptions", name="inscription")
*/
public function inscriptions(Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder) {
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$hash = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($hash);
$manager->persist($user);
$manager->flush();
return $this->redirectToRoute('login');
}
return $this->render('compte/inscriptions.html.twig', [
'form' => $form->createView()
]);
}
User.php :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields={"email"},
* message="L'email que vous avez indiqué est déja utilisé !"
* )
*/
class User implements UserInterface
{
听起来您只是将接口导入本地名称空间,但并未实际实现它。
namespace foo; // namespace declaration
use Bar\UserInterface; // namespace import, does not actually implement it.
class User implements UserInterface { // <- this is the important part
// now you need to implement the methods specified in the interface.
}
或者,假设 class Bar\User
实现 Bar\UserInterface
:
class User extends Bar\User {
// now you only need to define/redefine the specific things that you want to
}
这是错误
Argument 1 passed to Symfony\Component\Security\Core\Encoder\UserPasswordEncoder::encodePassword() must be an instance of Symfony\Component\Security\Core\User\UserInterface, instance of App\Entity\User given, called in C:\Users\willi\Desktop\Crowdin\src\Controller\CompteController.php on line 44
CompteController.php :
/**
* @Route("/inscriptions", name="inscription")
*/
public function inscriptions(Request $request, ObjectManager $manager, UserPasswordEncoderInterface $encoder) {
$user = new User();
$form = $this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$hash = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($hash);
$manager->persist($user);
$manager->flush();
return $this->redirectToRoute('login');
}
return $this->render('compte/inscriptions.html.twig', [
'form' => $form->createView()
]);
}
User.php :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @UniqueEntity(
* fields={"email"},
* message="L'email que vous avez indiqué est déja utilisé !"
* )
*/
class User implements UserInterface
{
听起来您只是将接口导入本地名称空间,但并未实际实现它。
namespace foo; // namespace declaration
use Bar\UserInterface; // namespace import, does not actually implement it.
class User implements UserInterface { // <- this is the important part
// now you need to implement the methods specified in the interface.
}
或者,假设 class Bar\User
实现 Bar\UserInterface
:
class User extends Bar\User {
// now you only need to define/redefine the specific things that you want to
}