src/Controller/RegistrationController.php line 24

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