src/Controller/RegistrationController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Repository\ChocolaterieRepository;
  5. use App\Form\RegistrationFormType;
  6. use App\Security\UserAuthenticator;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. class RegistrationController extends AbstractController
  16. {
  17.     #[Route('/inscription'name'app_register')]
  18.     //Route pour accĂ©der au formulaire admin 
  19.     #[Route('/inscription/admin'name'app_register_admin')]
  20.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  21.     {
  22.         $routeName $request->attributes->get('_route');
  23.         $user = new User();
  24.         $form $this->createForm(RegistrationFormType::class, $user);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             // encode the plain password
  28.             $user->setPassword(
  29.                 $userPasswordHasher->hashPassword(
  30.                     $user,
  31.                     $form->get('plainPassword')->getData()
  32.                 )
  33.             );
  34.             $user->setImageProfil('img/profils/default/Cranks-1.png');
  35.             $user->setImageProfilAlt('Image de profil sans genre, de couleur de peau orange avec un oeil bleu et des cheveux bleu sur fond bleu-ciel');
  36.             $user->setImageBandeau('img/profils/default/pexels-rovenimagescom-949587.jpg');
  37.             $user->setImageBandeauAlt('Fond blanc lumineux');
  38.             $user->setCreatedAt(new \DateTimeImmutable());
  39.             if($routeName == "app_register_admin"){
  40.                 $user->setRoles(["ROLE_ADMIN"]);
  41.             } else if ($routeName == "app_register"){
  42.                 $user->setRoles(["ROLE_USER"]);
  43.             }
  44.             $entityManager->persist($user);
  45.             $entityManager->flush();
  46.             // do anything else you need here, like send an email
  47.             return $userAuthenticator->authenticateUser(
  48.                 $user,
  49.                 $authenticator,
  50.                 $request
  51.             );
  52.         }
  53.         return $this->render('security/register.html.twig', [
  54.             'registrationForm' => $form->createView(),
  55.             'request' => $routeName,
  56.         ]);
  57.     }
  58. }