src/Controller/RegistrationController.php line 24

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Coach;
  4. use App\Entity\Offre;
  5. use App\Entity\User;
  6. use App\Form\RegistrationFormType;
  7. use App\Security\LoginFormAuthenticator;
  8. use App\Service\EmailService;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     #[Route('/register'name'app_register')]
  20.     public function register(EntityManagerInterface $em,EmailService $email,Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorLoginFormAuthenticator $authenticatorEntityManagerInterface $entityManager,ValidatorInterface $validator): Response
  21.     {
  22.         $user = new User();
  23.         // OFFRE GRATUIT 1
  24.         $offre $em->getRepository(Offre::class)->find(1);
  25.         $coach $em->getRepository(Coach::class)->find(2);
  26.         $form $this->createForm(RegistrationFormType::class, $user);
  27.         $form->handleRequest($request);
  28.         if ($form->isSubmitted() && $form->isValid()) {
  29.             // encode the plain password
  30.                 $user->setPassword(
  31.                     $userPasswordHasher->hashPassword(
  32.                         $user,
  33.                         $form->get('password')->getData()
  34.                 )
  35.             );
  36.             $user->setRoles(['ROLE_USER_NO_VALIDE']);
  37.             $user->setOffre($offre);
  38.             $entityManager->persist($user);
  39.             $entityManager->flush();
  40.             // do anything else you need here, like send an email
  41.             $email->sendWelcomeMessage($user);
  42.             $this->addFlash(
  43.                 'success',
  44.                 '1'
  45.             );
  46.             return $this->redirectToRoute('app_login', ['error' => false]);
  47.         }
  48.         return $this->render('registration/register.html.twig', [
  49.             'registrationForm' => $form,
  50.         ]);
  51.     }
  52. }