src/Controller/App/RegistrationController.php line 35

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