src/Controller/RegistrationController.php line 20

  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. class RegistrationController extends AbstractController
  15. {
  16.     #[Route('/inscription'name'app_register')]
  17.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppCustomAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  18.     {
  19.         $user = new User();
  20.         $form $this->createForm(RegistrationFormType::class, $user);
  21.         $form->handleRequest($request);
  22.         if ($form->isSubmitted() && $form->isValid()) {
  23.             // encode the plain password
  24.             $user->setPassword(
  25.                 $userPasswordHasher->hashPassword(
  26.                     $user,
  27.                     $form->get('plainPassword')->getData()
  28.                 )
  29.             );
  30.             $entityManager->persist($user);
  31.             $entityManager->flush();
  32.             $message '<b>Félicitations</b> pour votre première connexion !<br>' .
  33.                 'De grandes choses vous attendent.';
  34.             $this->addFlash('success'$message);
  35.             return $userAuthenticator->authenticateUser(
  36.                 $user,
  37.                 $authenticator,
  38.                 $request
  39.             );
  40.         }
  41.         return $this->render('registration/register.html.twig', [
  42.             'registrationForm' => $form->createView(),
  43.         ]);
  44.     }
  45. }