src/Controller/RegistrationController.php line 31

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 SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. class RegistrationController extends AbstractController
  17. {
  18.     private EmailVerifier $emailVerifier;
  19.     public function __construct(EmailVerifier $emailVerifier)
  20.     {
  21.         $this->emailVerifier $emailVerifier;
  22.     }
  23.     /**
  24.      * @Route("/register", name="app_register")
  25.      */
  26.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  27.     {
  28.         $user = new User();
  29.         $form $this->createForm(RegistrationFormType::class, $user);
  30.         $form->handleRequest($request);
  31.         if ($form->isSubmitted() && $form->isValid()) {
  32.             // encode the plain password
  33.             $user->setPassword(
  34.             $userPasswordHasher->hashPassword(
  35.                     $user,
  36.                     $form->get('plainPassword')->getData()
  37.                 )
  38.             );
  39.             $entityManager->persist($user);
  40.             $entityManager->flush();
  41.             // generate a signed url and email it to the user
  42.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  43.                 (new TemplatedEmail())
  44.                     ->from(new Address($this->getParameter('app.mail_from_email'), $this->getParameter('app.mail_from_name')))
  45.                     ->to($user->getEmail())
  46.                     ->subject('Please Confirm your Email')
  47.                     ->htmlTemplate('email/confirmation.html.twig')
  48.             );
  49.             $this->addFlash('success''An email verification for your new account is sent!');
  50.             return $this->redirectToRoute('homepage');
  51.         }
  52.         return $this->render('/client/registration/register.html.twig', [
  53.             'registrationForm' => $form->createView(),
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/verify/email", name="app_verify_email")
  58.      */
  59.     public function verifyUserEmail(Request $requestUserRepository $userRepository): Response
  60.     {
  61.         $id $request->get('id');
  62.         if (null === $id) {
  63.             return $this->redirectToRoute('app_register');
  64.         }
  65.         $user $userRepository->find($id);
  66.         if (null === $user) {
  67.             return $this->redirectToRoute('app_register');
  68.         }
  69.         // validate email confirmation link, sets User::isVerified=true and persists
  70.         try {
  71.             $this->emailVerifier->handleEmailConfirmation($request$user);
  72.         } catch (VerifyEmailExceptionInterface $exception) {
  73.             $this->addFlash('verify_email_error'$exception->getReason());
  74.             return $this->redirectToRoute('app_register');
  75.         }
  76.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  77.         $this->addFlash('success''Your email address has been verified.');
  78.         return $this->redirectToRoute('app_login');
  79.     }
  80. }