src/Controller/RegistrationController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. class RegistrationController extends AbstractController
  13. {
  14.     #[Route('/register'name'app_register')]
  15.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  16.     {
  17.         $user = new User();
  18.         $form $this->createForm(RegistrationFormType::class, $user);
  19.         $form->handleRequest($request);
  20.         if ($form->isSubmitted() && $form->isValid()) {
  21.             // encode the plain password
  22.             $user->setPassword(
  23.                 $userPasswordHasher->hashPassword(
  24.                     $user,
  25.                     $form->get('plainPassword')->getData()
  26.                 )
  27.             );
  28.             $entityManager->persist($user);
  29.             $entityManager->flush();
  30.             // do anything else you need here, like send an email
  31.             return $this->redirectToRoute('app_login');
  32.         }
  33.         return $this->render('registration/register.html.twig', [
  34.             'registrationForm' => $form->createView(),
  35.         ]);
  36.     }
  37. }