src/Controller/RegistrationController.php line 23

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserInfos;
  5. use App\Form\RegistrationFormType;
  6. use App\Security\AppAuthenticator;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\String\Slugger\SluggerInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. class RegistrationController extends AbstractController
  18. {
  19.     #[Route('/register'name'app_register')]
  20.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppAuthenticator $authenticatorEntityManagerInterface $entityManagerSluggerInterface $slugger): Response
  21.     {
  22.         $user = new User();
  23.         $userInfos = new UserInfos();
  24.         $form $this->createForm(RegistrationFormType::class, $user);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             $imageFile $form->get('UserInfos')->get('image')->getData();
  28.             if ($imageFile) {
  29.                 $originalFilename pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
  30.                 $safeFilename $slugger->slug($originalFilename);
  31.                 $newFilename $safeFilename '-' uniqid() . '.' $imageFile->guessExtension();
  32.                 try {
  33.                     $imageFile->move(
  34.                         $this->getParameter('images_directory'), // définir ce paramètre dans votre fichier de configuration services.yaml
  35.                         $newFilename
  36.                     );
  37.                 } catch (FileException $e) {
  38.                     // gérer l'exception si quelque chose se passe mal pendant l'upload du fichier
  39.                 }
  40.                 $userInfos->setImage($newFilename);
  41.             }
  42.             // encode the plain password
  43.             $user->setPassword(
  44.                 $userPasswordHasher->hashPassword(
  45.                     $user,
  46.                     $form->get('plainPassword')->getData()
  47.                 )
  48.             );
  49.             $userInfos->setName($form->get('UserInfos')->get('name')->getData());
  50.             $user->setUserInfos($userInfos);
  51.             $entityManager->persist($user);
  52.             $entityManager->persist($userInfos);
  53.             $entityManager->flush();
  54.             return $userAuthenticator->authenticateUser(
  55.                 $user,
  56.                 $authenticator,
  57.                 $request
  58.             );
  59.         }
  60.         return $this->render('registration/register.html.twig', [
  61.             'registrationForm' => $form->createView(),
  62.         ]);
  63.     }
  64. }