src/Controller/RegistrationController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Security\EmailVerifier;
  5. use App\Form\RegistrationFormType;
  6. use App\Repository\UserRepository;
  7. use Symfony\Component\Mime\Address;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     private $emailVerifier;
  20.     public function __construct(EmailVerifier $emailVerifierParameterBagInterface $params)
  21.     {
  22.         $this->emailVerifier $emailVerifier;
  23.         $this->params $params;
  24.     }
  25.     /**
  26.      * @Route("/register", name="app_register")
  27.      */
  28.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherInterfaceTranslatorInterface $translator): Response
  29.     {
  30.         $user = new User();
  31.         $form $this->createForm(RegistrationFormType::class, $user);
  32.         $form->handleRequest($request);
  33.         if ($form->isSubmitted() && $form->isValid()) {
  34.             // encode the plain password
  35.             $user->setPassword(
  36.             $userPasswordHasherInterface->hashPassword(
  37.                     $user,
  38.                     $form->get('plainPassword')->getData()
  39.                 )
  40.             );
  41.             //set active lang
  42.             $user->setLang($request->getLocale());
  43.             $entityManager $this->getDoctrine()->getManager();
  44.             $entityManager->persist($user);
  45.             $entityManager->flush();
  46.             // generate a signed url and email it to the user
  47.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  48.                 (new TemplatedEmail())
  49.                     ->from(new Address($this->params->get('app.sendermail'), $this->params->get('app.title')))
  50.                     ->to($user->getEmail())
  51.                     ->subject($translator->trans('register.subject', [], 'email'))
  52.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  53.             );
  54.             return $this->redirectToRoute('app_register_verify');
  55.         }
  56.         return $this->render('registration/register.html.twig', [
  57.             'registrationForm' => $form->createView(),
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/verify/email", name="app_verify_email")
  62.      */
  63.     public function verifyUserEmail(Request $requestUserRepository $userRepositoryTranslatorInterface $translator): Response
  64.     {
  65.         $id $request->get('id'); // retrieve the user id from the url
  66.         // Verify the user id exists and is not null
  67.         if (null === $id) {
  68.             return $this->redirectToRoute('app_register');
  69.         }
  70.         $user $userRepository->find($id);
  71.         // Ensure the user exists in persistence
  72.         if (null === $user) {
  73.             return $this->redirectToRoute('app_register');
  74.         }
  75.         // validate email confirmation link, sets User::isVerified=true and persists
  76.         try {
  77.             $this->emailVerifier->handleEmailConfirmation($request$user);
  78.         } catch (VerifyEmailExceptionInterface $exception) {
  79.             $this->addFlash('verify_email_error'$exception->getReason());
  80.             return $this->redirectToRoute('app_register');
  81.         }
  82.         // flash and redirect
  83.         $this->addFlash('success'$translator->trans('register.confirm'));
  84.         return $this->redirectToRoute('app_login');
  85.     }
  86.     /**
  87.      * @Route("/register/verify", name="app_register_verify")
  88.      */
  89.     public function verifyMsg(): Response
  90.     {
  91.         return $this->render('registration/confirm.html.twig');
  92.     }
  93. }