src/Controller/RegistrationController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\Frontend\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 Symfony\Contracts\Translation\TranslatorInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. class RegistrationController extends AbstractController
  17. {
  18.     private EmailVerifier $emailVerifier;
  19.     /**
  20.      * @param EmailVerifier $emailVerifier
  21.      */
  22.     public function __construct(EmailVerifier $emailVerifier)
  23.     {
  24.         $this->emailVerifier $emailVerifier;
  25.     }
  26.     /**
  27.      * @param Request                     $request
  28.      * @param UserPasswordHasherInterface $userPasswordHasher
  29.      * @param EntityManagerInterface      $entityManager
  30.      *
  31.      * @return Response
  32.      *
  33.      * @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
  34.      */
  35.     #[Route('/register'name'app_register')]
  36.     public function register(
  37.         Request $request,
  38.         UserPasswordHasherInterface $userPasswordHasher,
  39.         EntityManagerInterface $entityManager
  40.     ): Response {
  41.         $user = new User();
  42.         $form $this->createForm(RegistrationFormType::class, $user);
  43.         $form->handleRequest($request);
  44.         if ($form->isSubmitted() && $form->isValid()) {
  45.             // encode the plain password
  46.             $user->setPassword(
  47.                 $userPasswordHasher->hashPassword(
  48.                     $user,
  49.                     $form->get('plainPassword')->getData()
  50.                 )
  51.             );
  52.             $entityManager->persist($user);
  53.             $entityManager->flush();
  54.             // generate a signed url and email it to the user
  55.             $this->emailVerifier->sendEmailConfirmation(
  56.                 'app_verify_email',
  57.                 $user,
  58.                 (new TemplatedEmail())
  59.                     ->from(new Address('noreply@bandenmanager.de''Bandenmanager'))
  60.                     ->to($user->getEmail())
  61.                     ->subject('Please Confirm your Email')
  62.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  63.             );
  64.             // do anything else you need here, like send an email
  65.             return $this->redirectToRoute('_profiler_home');
  66.         }
  67.         return $this->render(
  68.             'registration/register.html.twig',
  69.             [
  70.             'registrationForm' => $form->createView(),
  71.             ]
  72.         );
  73.     }
  74.     /**
  75.      * @param Request             $request
  76.      * @param TranslatorInterface $translator
  77.      *
  78.      * @return Response
  79.      */
  80.     #[Route('/verify/email'name'app_verify_email')]
  81.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  82.     {
  83.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  84.         // validate email confirmation link, sets User::isVerified=true and persists
  85.         try {
  86.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  87.         } catch (VerifyEmailExceptionInterface $exception) {
  88.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  89.             return $this->redirectToRoute('app_register');
  90.         }
  91.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  92.         $this->addFlash('success''Your email address has been verified.');
  93.         return $this->redirectToRoute('app_register');
  94.     }
  95. }