src/Controller/Front/RegistrationController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\LoginFormAuthenticator;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  12. class RegistrationController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/register", name="app_register")
  16.      */
  17.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderGuardAuthenticatorHandler $guardHandlerLoginFormAuthenticator $authenticator): 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.                 $passwordEncoder->encodePassword(
  26.                     $user,
  27.                     $form->get('plainPassword')->getData()
  28.                 )
  29.             );
  30.             $entityManager $this->getDoctrine()->getManager();
  31.             $entityManager->persist($user);
  32.             $entityManager->flush();
  33.             // do anything else you need here, like send an email
  34.             return $guardHandler->authenticateUserAndHandleSuccess(
  35.                 $user,
  36.                 $request,
  37.                 $authenticator,
  38.                 'main' // firewall name in security.yaml
  39.             );
  40.         }
  41.         return $this->render('registration/register.html.twig', [
  42.             'registrationForm' => $form->createView(),
  43.         ]);
  44.     }
  45. }