src/Controller/RegistrationController.php line 53

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('/inscription'name'app_register')]
  17.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorUserAuthenticator $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.             $user->setRoles(["ROLE_USER"]);
  31.             $user->setFirstname($form->get('firstname')->getData());
  32.             $user->setLastname($form->get('lastname')->getData());             
  33.             $user->setPhone'+33'.substr($form->get('phone')->getData(), 110) );
  34.             $user->setCompany($form->get('company')->getData());
  35.             $user->setCreatedAt(new \DateTimeImmutable());
  36.             $user->setLastLoginAt(new \DateTimeImmutable());
  37.             
  38.             $entityManager->persist($user);
  39.             $entityManager->flush();
  40.             // do anything else you need here, like send an email
  41.             return $userAuthenticator->authenticateUser(
  42.                 $user,
  43.                 $authenticator,
  44.                 $request
  45.             );
  46.         }
  47.         return $this->render('registration/register.html.twig', [
  48.             'registrationForm' => $form->createView(),
  49.         ]);
  50.     }
  51. }