src/Controller/RegistrationController.php line 26

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Personne;
  4. use App\Entity\User;
  5. use App\Form\CompleteProfilType;
  6. use App\Form\RegistrationFormType;
  7. use App\Repository\UserRepository;
  8. use App\Security\UserAuthenticator;
  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\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Address;
  15. use Symfony\Component\Mime\Email;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class RegistrationController extends AbstractController
  21. {
  22.     #[Route('/register'name'app_register')]
  23.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  24.     {
  25.         $user = new User();
  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('plainPassword')->getData()
  34.                 )
  35.             );
  36.             $user->setLog(1);
  37.             $entityManager->persist($user);
  38.             $entityManager->flush();
  39.             // do anything else you need here, like send an email
  40.             return $userAuthenticator->authenticateUser(
  41.                 $user,
  42.                 $authenticator,
  43.                 $request
  44.             );
  45.         }
  46.         return $this->render('registration/register.html.twig', [
  47.             'registrationForm' => $form->createView(),
  48.         ]);
  49.     }
  50.     #[Route('/completer/profile'name'app_completer_profile')]
  51.     public function completerProfile(UserRepository $userRepositoryRequest $requestEntityManagerInterface $entityManager,MailerInterface $mailer): Response
  52.     {
  53.         $user $userRepository->find($this->getUser()->getId());
  54.         $personne=new Personne();// Remplacez cela par votre moyen d'obtenir l'utilisateur en cours
  55.         $form $this->createForm(CompleteProfilType::class,$personne );
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             $user->setOneConnexion(true);
  59.             $personne->setUser($user);
  60.             $entityManager->persist($user);
  61.             $entityManager->persist($personne);
  62.             $entityManager->flush();
  63.             $this->addFlash('success''Profil mis à jour avec succès !');
  64.             return $this->redirectToRoute('app_dashboard');
  65.         }
  66.         return $this->render('registration/completeProfile.html.twig', [
  67.                  'form'=> $form
  68.         ]);
  69.     }
  70. }