src/Controller/RegistrationController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AppUser;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\UserAuthenticator;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\CssSelector\XPath\TranslatorInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Mailer\MailerInterface;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19. public function __construct(
  20. private EntityManagerInterface $entityManager,
  21. private MailerInterface $mailer
  22. ) {
  23. }
  24. #[Route(path: '/register', name: 'app_register')]
  25. public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, UserAuthenticator $authenticator): Response
  26. {
  27. $user = new AppUser();
  28. $form = $this->createForm(RegistrationFormType::class, $user);
  29. $form->handleRequest($request);
  30. if ($form->isSubmitted() && $form->isValid()) {
  31. // encode the plain password
  32. $user->setPassword(
  33. $userPasswordHasher->hashPassword(
  34. $user,
  35. $form->get('plainPassword')->getData()
  36. )
  37. );
  38. $this->entityManager->persist($user);
  39. $this->entityManager->flush();
  40. // send confirmation email
  41. $email = (new TemplatedEmail())
  42. ->from(new Address('suture_web@youranastomosis.com', 'Youranastomosis'))
  43. ->to($user->getEmail())
  44. ->subject('User account created')
  45. ->htmlTemplate('registration/email.html.twig')
  46. ;
  47. $this->mailer->send($email);
  48. return $userAuthenticator->authenticateUser(
  49. $user,
  50. $authenticator,
  51. $request
  52. );
  53. }
  54. return $this->render('registration/register.html.twig', [
  55. 'registrationForm' => $form->createView(),
  56. ]);
  57. }
  58. }