src/Controller/RegistrationController.php line 23
<?phpnamespace App\Controller;use App\Entity\User;use App\Entity\UserInfos;use App\Form\RegistrationFormType;use App\Security\AppAuthenticator;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\String\Slugger\SluggerInterface;use Symfony\Contracts\Translation\TranslatorInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\File\Exception\FileException;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;class RegistrationController extends AbstractController{#[Route('/register', name: 'app_register')]public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppAuthenticator $authenticator, EntityManagerInterface $entityManager, SluggerInterface $slugger): Response{$user = new User();$userInfos = new UserInfos();$form = $this->createForm(RegistrationFormType::class, $user);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$imageFile = $form->get('UserInfos')->get('image')->getData();if ($imageFile) {$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);$safeFilename = $slugger->slug($originalFilename);$newFilename = $safeFilename . '-' . uniqid() . '.' . $imageFile->guessExtension();try {$imageFile->move($this->getParameter('images_directory'), // définir ce paramètre dans votre fichier de configuration services.yaml$newFilename);} catch (FileException $e) {// gérer l'exception si quelque chose se passe mal pendant l'upload du fichier}$userInfos->setImage($newFilename);}// encode the plain password$user->setPassword($userPasswordHasher->hashPassword($user,$form->get('plainPassword')->getData()));$userInfos->setName($form->get('UserInfos')->get('name')->getData());$user->setUserInfos($userInfos);$entityManager->persist($user);$entityManager->persist($userInfos);$entityManager->flush();return $userAuthenticator->authenticateUser($user,$authenticator,$request);}return $this->render('registration/register.html.twig', ['registrationForm' => $form->createView(),]);}}