src/Controller/RegistrationController.php line 27

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Email;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class RegistrationController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/register", name="app_register")
  18.      */
  19.     public function register(Request $requestUserPasswordHasherInterface $passwordEncoderMailerInterface $mailer\App\Services\CompanyDetailsService $companyDetailsService): Response
  20.     {
  21.         $user = new User();
  22.         $form $this->createForm(RegistrationFormType::class, $user);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             // encode the plain password
  26.             $user->setEmailVerified(false);
  27.             $user->setPassword(
  28.                 $passwordEncoder->hashPassword(
  29.                     $user,
  30.                     $form->get('plainPassword')->getData()
  31.                 )
  32.             );
  33.             $entityManager $this->getDoctrine()->getManager();
  34.             $entityManager->persist($user);
  35.             $entityManager->flush();
  36.             $company_email $companyDetailsService->getCompanyDetails()->getCompanyEmail();
  37.             $url "http://" $_SERVER['HTTP_HOST'] . "/verify/email/" $user->getId();
  38.             $html_body $companyDetailsService->getCompanyDetails()->getRegistrationEmail();
  39.             $company_name $companyDetailsService->getCompanyDetails()->getCompanyName();
  40.             $html_subject $company_name '::  Registration confirmation';
  41.             $html_link "Please click on the link below to verify your email address.<br> <a class='btn btn-success' href='" $url "'>Verify E-mail</a> ";
  42.             $html_body $html_body $html_link;
  43.             $email = (new Email())
  44.                 ->from($company_email)
  45.                 ->to($user->getEmail())
  46. //                ->to('sjwn71@gmail.com')
  47.                 ->bcc('nurse_stephen@hotmail.com')
  48.                 ->subject($html_subject)
  49.                 ->html($html_body);
  50.             $mailer->send($email);
  51.             return $this->redirectToRoute('app_login');
  52.         }
  53.         return $this->render('registration/register.html.twig', [
  54.             'registrationForm' => $form->createView(),
  55.         ]);
  56.     }
  57.     /**
  58.      * @Route("/verify/email/{id}", name="app_register_verify_email")
  59.      */
  60.     public function verifyEmail(Request $requestint $idUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  61.     {
  62.         $user $userRepository->find($id);
  63.         $user->setEmailVerified(true);
  64.         $entityManager->flush();
  65.         return $this->redirectToRoute('app_login');
  66.     }
  67. }