src/Controller/RegistrationController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Chercheur;
  4. use App\Entity\User;
  5. use App\Form\RegistrationFormType;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use DateTimeZone;
  14. use DateTime;
  15. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  16. use Cocur\Slugify\Slugify;
  17. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\Mime\Address;
  20. class RegistrationController extends AbstractController
  21. {
  22.     #[Route('/register'name'app_register')]
  23.     public function register(Request $request,MailerInterface $mailerUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  24.     {
  25.         $user = new Chercheur();
  26.         $user->setRoles(['ROLE_CHERCHEUR']);
  27.         $form $this->createForm(RegistrationFormType::class, $user);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             $user->setDateinscription(new DateTime('now', new DateTimeZone('Africa/Tunis')));
  31.             // encode the plain password
  32.             $user->setPassword(
  33.                 $userPasswordHasher->hashPassword(
  34.                     $user,
  35.                     $form->get('plainPassword')->getData()
  36.                 )
  37.             );
  38.             $photo $form->get('photo')->getData();
  39.             if ( $photo) {
  40.                 $originalLogoname pathinfo$photo->getClientOriginalName(), PATHINFO_FILENAME);
  41.                 $slugify = new Slugify();
  42.                 $a=  $slugify->slugify($originalLogoname, ['separator' => '-']);
  43.                 $newLogoname =  $a.'-'.uniqid().'.'.$photo->guessExtension();
  44.                 // Move the file to the directory where brochures are stored
  45.                 try {
  46.                     $photo->move(
  47.                         $this->getParameter('chercheur_directory'),
  48.                         $newLogoname
  49.                     );
  50.                 } catch (FileException $e) {
  51.                     // ... handle exception if something happens during file upload
  52.                 }
  53.                 $user->setPhoto($newLogoname);
  54.             }
  55.             $entityManager->persist($user);
  56.             $entityManager->flush();
  57.             // do anything else you need here, like send an email
  58.             
  59.             $emailMessage = (new TemplatedEmail())
  60.                 ->from(new Address('rfmi@smart-it-partner.com''RFMI'))
  61.                 ->to($user->getEmail())
  62.                 ->subject('Welcome! Registration successfully completed for RFMI 2026')
  63.                 ->htmlTemplate('registration/email_Inscri.html.twig')
  64.                 ->context([
  65.                     'nom' => $user->getNom(),
  66.                     'prenom' => $user->getNom(),
  67.                     'email_chercheur' => $user->getEmail(),
  68.                     'password' => $form->get('plainPassword')->getData(),
  69.                     
  70.                 ]);
  71.             $mailer->send($emailMessage);
  72.             return $this->redirectToRoute('app_login');
  73.         }
  74.         return $this->render('registration/register.html.twig', [
  75.             'registrationForm' => $form->createView(),
  76.         ]);
  77.     }
  78. }