src/Controller/RegistrationController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\contact;
  5. use App\Form\RegistrationFormType;
  6. use App\Message\SendEmailMessage;
  7. use App\Security\LoginFormAuthenticator;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  16. class RegistrationController extends AbstractController
  17. {
  18. /**
  19. * @Route("/register", name="app_register")
  20. */
  21. public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, LoginFormAuthenticator $authenticator, EntityManagerInterface $entityManager, MessageBusInterface $bus): Response
  22. {
  23. $user = new User();
  24. $csrfToken = $this->get('security.csrf.token_manager')->getToken('register')->getValue();
  25. $form = $this->createForm(RegistrationFormType::class, $user, [
  26. 'csrf_token' => $csrfToken,
  27. ]);
  28. $form->handleRequest($request);
  29. if ($form->isSubmitted() && $form->isValid()) {
  30. $contact = (new Contact())
  31. ->setEmail($user->getEMail())
  32. ->setUser($user);
  33. $user->setContact($contact);
  34. // encode the plain password
  35. $user->setPassword(
  36. $userPasswordHasher->hashPassword(
  37. $user,
  38. $form->get('plainPassword')->getData()
  39. )
  40. );
  41. $entityManager->persist($user);
  42. $entityManager->flush();
  43. // do anything else you need here, like send an email
  44. $bus->dispatch(new SendEmailMessage('validation_registration', '', $user->getId(),$user->getEmail()));
  45. return $userAuthenticator->authenticateUser(
  46. $user,
  47. $authenticator,
  48. $request
  49. );
  50. }
  51. return $this->render('registration/register.html.twig', [
  52. 'registrationForm' => $form->createView(),
  53. ]);
  54. }
  55. }