src/Controller/RegistrationController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use App\Security\EmailVerifier;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mime\Address;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     private EmailVerifier $emailVerifier;
  20.     public function __construct(EmailVerifier $emailVerifier)
  21.     {
  22.         $this->emailVerifier $emailVerifier;
  23.     }
  24.     #[Route('/register'name'app_register')]
  25.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  26.     {
  27.         $user = new User();
  28.         $form $this->createForm(RegistrationFormType::class, $user);
  29.         $form->handleRequest($request);
  30.         if ($form->isSubmitted() && $form->isValid()) {
  31.             // encode the plain password
  32.             $user->setPassword(
  33.                 $userPasswordHasher->hashPassword(
  34.                     $user,
  35.                     $form->get('plainPassword')->getData()
  36.                 )
  37.             );
  38.             $entityManager->persist($user);
  39.             $entityManager->flush();
  40.             // generate a signed url and email it to the user
  41.             $this->emailVerifier->sendEmailConfirmation(
  42.                 'app_verify_email',
  43.                 $user,
  44.                 (new TemplatedEmail())
  45.                     ->from(new Address('admin@paris-prestige-transfert.fr''Chennuo'))
  46.                     ->to($user->getEmail())
  47.                     ->subject('Please Confirm your Email')
  48.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  49.             );
  50.             // do anything else you need here, like send an email
  51.             return $this->redirectToRoute('app_home');
  52.         }
  53.         return $this->render('registration/register.html.twig', [
  54.             'registrationForm' => $form->createView(),
  55.         ]);
  56.     }
  57.     #[Route('/verify/email'name'app_verify_email')]
  58.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  59.     {
  60.         $id $request->get('id');
  61.         if (null === $id) {
  62.             return $this->redirectToRoute('app_register');
  63.         }
  64.         $user $userRepository->find($id);
  65.         if (null === $user) {
  66.             return $this->redirectToRoute('app_register');
  67.         }
  68.         // validate email confirmation link, sets User::isVerified=true and persists
  69.         try {
  70.             $this->emailVerifier->handleEmailConfirmation($request$user);
  71.         } catch (VerifyEmailExceptionInterface $exception) {
  72.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  73.             return $this->redirectToRoute('app_login');
  74.         }
  75.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  76.         $this->addFlash('success''Your email address has been verified.');
  77.         return $this->redirectToRoute('app_login');
  78.     }
  79. }