src/Controller/RegistrationController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\AppCustomAuthenticator;
  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\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  15. use Symfony\Component\HttpFoundation\File\UploadedFile;
  16. use Symfony\Component\String\Slugger\SluggerInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     #[Route('/register'name'app_register')]
  20.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppCustomAuthenticator $authenticatorEntityManagerInterface $entityManagerSluggerInterface $slugger): Response
  21.     {
  22.         $user = new User();
  23.         $form $this->createForm(RegistrationFormType::class, $user);
  24.         $form->handleRequest($request);
  25.         if ($form->isSubmitted() && $form->isValid()) {
  26.             // encode the plain password
  27.             $role = array($form->get('roles')->getData());
  28.             $user->setRoles($role[0]);
  29.             $user->setPassword(
  30.                 $userPasswordHasher->hashPassword(
  31.                     $user,
  32.                     $form->get('plainPassword')->getData()
  33.                 )
  34.             );
  35.             $cvFile $form->get('cv')->getData();
  36.             // this condition is needed because the 'brochure' field is not required
  37.             // so the PDF file must be processed only when a file is uploaded
  38.             if ($cvFile) {
  39.                 $originalFilename pathinfo($cvFile->getClientOriginalName(), PATHINFO_FILENAME);
  40.                 // this is needed to safely include the file name as part of the URL
  41.                 $safeFilename $slugger->slug($originalFilename);
  42.                 $newFilename $safeFilename.'-'.uniqid().'.'.$cvFile->guessExtension();
  43.                 // Move the file to the directory where brochures are stored
  44.                 try {
  45.                     $cvFile->move(
  46.                         $this->getParameter('app.path.cv'),
  47.                         $newFilename
  48.                     );
  49.                 } catch (FileException $e) {
  50.                     // ... handle exception if something happens during file upload
  51.                 }
  52.                 // updates the 'brochureFilename' property to store the PDF file name
  53.                 // instead of its contents
  54.                 $user->setCv($newFilename);
  55.             }
  56.             $photoDeProfilFile $form->get('photoDeProfil')->getData();
  57.             // this condition is needed because the 'brochure' field is not required
  58.             // so the PDF file must be processed only when a file is uploaded
  59.             if ($photoDeProfilFile) {
  60.                 $originalFilename pathinfo($photoDeProfilFile->getClientOriginalName(), PATHINFO_FILENAME);
  61.                 // this is needed to safely include the file name as part of the URL
  62.                 $safeFilename $slugger->slug($originalFilename);
  63.                 $newFilename $safeFilename.'-'.uniqid().'.'.$photoDeProfilFile->guessExtension();
  64.                 // Move the file to the directory where brochures are stored
  65.                 try {
  66.                     $photoDeProfilFile->move(
  67.                         $this->getParameter('app.path.photo'),
  68.                         $newFilename
  69.                     );
  70.                 } catch (FileException $e) {
  71.                     // ... handle exception if something happens during file upload
  72.                 }
  73.                 // updates the 'brochureFilename' property to store the PDF file name
  74.                 // instead of its contents
  75.                 $user->setPhotoDeProfil($newFilename);
  76.             }
  77.             $entityManager->persist($user);
  78.             $entityManager->flush();
  79.             // do anything else you need here, like send an email
  80.             return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  81.         }
  82.         return $this->render('registration/register.html.twig', [
  83.             'registrationForm' => $form->createView(),
  84.         ]);
  85.     }
  86. }