src/Controller/RegistrationController.php line 51

  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. class RegistrationController extends AbstractController
  14. {
  15.     #[Route('/register'name'app_register')]
  16.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  17.     {
  18.         $user = new User();
  19.         $form $this->createForm(RegistrationFormType::class, $user);
  20.         $form->handleRequest($request);
  21.         if ($form->isSubmitted() && $form->isValid()) {
  22.             // encode the plain password
  23.             $user->setPassword(
  24.                 $userPasswordHasher->hashPassword(
  25.                     $user,
  26.                     $form->get('plainPassword')->getData()
  27.                 )
  28.             );
  29.             $entityManager->persist($user);
  30.             $entityManager->flush();
  31.             // do anything else you need here, like send an email
  32.             return $userAuthenticator->authenticateUser(
  33.                 $user,
  34.                 $authenticator,
  35.                 $request
  36.             );
  37.         }
  38.         return $this->render('registration/register.html.twig', [
  39.             'registrationForm' => $form->createView(),
  40.         ]);
  41.     }
  42.     #[Route('/register/teacher'name'app_register_teacher')]
  43.     public function registerTeacher(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  44.     {
  45.         $user = new User();
  46.         $form $this->createForm(RegistrationFormType::class, $user);
  47.         $form->handleRequest($request);
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             $user->setRoles(["ROLE_TEACHER"]);
  50.             $user->setmainJobPlace($form->get('mainJobPlace')->getData());
  51.             $user->setEducationalLevel($form->get('educationalLevel')->getData());
  52.             // encode the plain password
  53.             $user->setPassword(
  54.                 $userPasswordHasher->hashPassword(
  55.                     $user,
  56.                     $form->get('plainPassword')->getData()
  57.                 )
  58.             );
  59.             $entityManager->persist($user);
  60.             $entityManager->flush();
  61.             // do anything else you need here, like send an email
  62.             return $userAuthenticator->authenticateUser(
  63.                 $user,
  64.                 $authenticator,
  65.                 $request
  66.             );
  67.         }
  68.         return $this->render('registration/register_teacher.html.twig', [
  69.             'registrationForm' => $form->createView(),
  70.         ]);
  71.     }
  72. }