src/Controller/RegistrationController.php line 26

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\Routing\Annotation\Route;
  14. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  15. class RegistrationController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/register", name="app_register")
  19.      */
  20.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderMailerInterface $mailer,\App\Services\CompanyDetails $companyDetails): Response
  21.     {
  22.         $user = new User();
  23.         $form $this->createForm(RegistrationFormType::class, $user);
  24.         $form->handleRequest($request);
  25.         if ($form->isSubmitted() && $form->isValid()) {
  26.             // encode the plain password
  27.             $user->setEmailVerified(false);
  28.             $user->setPassword(
  29.                 $passwordEncoder->encodePassword(
  30.                     $user,
  31.                     $form->get('plainPassword')->getData()
  32.                 )
  33.             );
  34.             $entityManager $this->getDoctrine()->getManager();
  35.             $entityManager->persist($user);
  36.             $entityManager->flush();
  37.             $company_email $companyDetails->getCompanyDetails()->getCompanyEmail();
  38.             // do anything else you need here, like send an email
  39.             $url "http://".$_SERVER['HTTP_HOST']."/verify/email/".$user->getId();
  40.             $html "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> ";
  41.             $email = (new Email())
  42.                 ->from($company_email)
  43. //                ->to($user->getEmail())
  44.                 ->to('sjwn71@gmail.com')
  45.                 ->bcc('nurse_stephen@hotmail.com')
  46.                 ->subject("Verify Mail")
  47.                 ->html($html);
  48.             $mailer->send($email);
  49.             return $this->redirectToRoute('app_login');
  50.         }
  51.         return $this->render('registration/register.html.twig', [
  52.             'registrationForm' => $form->createView(),
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/verify/email/{id}", name="app_register_verify_email")
  57.      */
  58.     public function verifyEmail($id,UserRepository $userRepository,EntityManagerInterface $entityManagerRequest $request): Response{
  59.        $user $userRepository->find($id);
  60.        $user->setEmailVerified(true);
  61.        $entityManager->flush();
  62.        return $this->redirectToRoute('app_login');
  63.     }
  64. }