src/Controller/RegistrationController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\UserAuthenticator;
  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('/register', name: 'app_register')]
  17.     public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, UserAuthenticator $authenticator, EntityManagerInterface $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.             // do anything else you need here, like send an email
  33.             return $userAuthenticator->authenticateUser(
  34.                 $user,
  35.                 $authenticator,
  36.                 $request
  37.             );
  38.         }
  39.         return $this->render('registration/register.html.twig', [
  40.             'registrationForm' => $form->createView(),
  41.             'mode' => 'Register'
  42.         ]);
  43.     }
  44.     #[Route('/user/{id}/edit', name: 'app_edit_user')]
  45.     public function edit_user(int $id, Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, UserAuthenticator $authenticator, EntityManagerInterface $entityManager): Response
  46.     {
  47.         $user = $entityManager->getRepository(User::class)->find($id);
  48.         $form = $this->createForm(RegistrationFormType::class, $user);
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             // encode the plain password
  52.             $user->setPassword(
  53.                 $userPasswordHasher->hashPassword(
  54.                     $user,
  55.                     $form->get('plainPassword')->getData()
  56.                 )
  57.             );
  58.             $entityManager->persist($user);
  59.             $entityManager->flush();
  60.             // do anything else you need here, like send an email
  61.             return $userAuthenticator->authenticateUser(
  62.                 $user,
  63.                 $authenticator,
  64.                 $request
  65.             );
  66.         }
  67.         return $this->render('registration/register.html.twig', [
  68.             'registrationForm' => $form->createView(),
  69.             'mode' => 'Edit'
  70.         ]);
  71.     }
  72. }