src/Controller/UserManagement/RegistrationController.php line 28

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