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 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.     /**
  15.      * @Route("/register", name="app_register")
  16.      */
  17.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $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.             $roles[] = 'ROLE_CLIENT';
  31.             $user->setRoles($roles);
  32.             $user->setArchive(0);
  33.             $user->setToken(md5(time()));
  34.             $entityManager->persist($user);
  35.             $entityManager->flush();
  36.             // do anything else you need here, like send an email
  37.             return $this->redirectToRoute('app_default');
  38.         }
  39.         return $this->render('registration/register.html.twig', [
  40.             'registrationForm' => $form->createView(),
  41.         ]);
  42.     }
  43. }