src/Controller/RegistrationController.php line 33
<?phpnamespace App\Controller;use App\Entity\User;use App\Form\RegistrationFormType;use App\Repository\UserRepository;use App\Security\EmailVerifier;use App\Security\LoginFormAuthenticator;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bridge\Twig\Mime\TemplatedEmail;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Mime\Address;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;use Symfony\Contracts\Translation\TranslatorInterface;use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;class RegistrationController extends AbstractController{private EmailVerifier $emailVerifier;public function __construct(EmailVerifier $emailVerifier, Private ParameterBagInterface $params){$this->emailVerifier = $emailVerifier;}#[Route('/register', name: 'app_register')]public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, LoginFormAuthenticator $authenticator, EntityManagerInterface $entityManager): Response{$user = new User();$form = $this->createForm(RegistrationFormType::class, $user);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {// encode the plain password$user->setRoles(['ROLE_USER']);$user->setPassword($userPasswordHasher->hashPassword($user,$form->get('plainPassword')->getData()));$entityManager->persist($user);$entityManager->flush();// générer une adresse URL signée et l'envoyer par courrier électronique à l'utilisateur$this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,(new TemplatedEmail())->from(new Address($this->params->get('no_reply_email'), $this->params->get('website_name')))->to($user->getEmail())->subject('Please Confirm your Email')->htmlTemplate('registration/confirmation_email.html.twig'));return $userAuthenticator->authenticateUser($user,$authenticator,$request);}return $this->render('registration/register.html.twig', ['registrationForm' => $form->createView(),]);}#[Route('/verify/email', name: 'app_verify_email')]public function verifyUserEmail(Request $request, TranslatorInterface $translator, UserRepository $userRepository): Response{$id = $request->get('id');if (null === $id) {return $this->redirectToRoute('app_register');}$user = $userRepository->find($id);if (null === $user) {return $this->redirectToRoute('app_register');}// validate email confirmation link, sets User::isVerified=true and persiststry {$this->emailVerifier->handleEmailConfirmation($request, $user);} catch (VerifyEmailExceptionInterface $exception) {$this->addFlash('verify_email_error', $translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));return $this->redirectToRoute('app_register');}// @TODO Change the redirect on success and handle or remove the flash message in your templates$this->addFlash('success', 'Votre adresse électronique a été vérifiée');return $this->redirectToRoute('app_register');}}