src/Controller/RegistrationController.php line 25

Open in your IDE?
  1. <?php
  2. // src/Controller/RegistrationController.php
  3. namespace App\Controller;
  4. use App\Entity\AppUser;
  5. use App\Form\RegistrationFormType;
  6. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13.  * @Route("/{_locale}/register")
  14.  */
  15. class RegistrationController extends AbstractController
  16. {
  17.     
  18.     /**
  19.      * @Route("/", name="registration_register")
  20.      * @return \Symfony\Component\HttpFoundation\Response
  21.     */
  22.     public function register(Request $request, UserPasswordHasherInterface $passwordHasher)
  23.     {
  24.         // ... e.g. get the user data from a registration form
  25.         $user = new AppUser;
  26.         $plaintextPassword = '';
  27.         $em = $this->getDoctrine()
  28.                     ->getManager();
  29.         
  30.         $form = $this->createForm(RegistrationFormType::class, $user);
  31.         if($request->getMethod() == 'POST'){
  32.              $form->handleRequest($request);
  33.             if($form->isValid()){
  34.                 $data = $form->getData();
  35.                     
  36.                 $plaintextPassword = '';
  37.                 // hash the password (based on the security.yaml config for the $user class)
  38.                 $hashedPassword = $passwordHasher->hashPassword($user, $data->getPlainPassword());
  39.                 $user->setPassword($hashedPassword);
  40.                 $em->persist($user);
  41.                 $em->flush();
  42.                 
  43.              return $this->redirectToRoute('registration_confirmed', array('username' => $user->getUsername()));
  44.             }
  45.         }
  46.         
  47.         return $this->render('registration/register.html.twig', [
  48.         
  49.             'form' => $form->createView()
  50.         ]);
  51.     }
  52.     /**
  53.      * @Route("/registration-confirmed/{username}", name="registration_confirmed")
  54.      * @return \Symfony\Component\HttpFoundation\Response
  55.     */
  56.     public function registrationConfirmedAction(Request $request, UserPasswordHasherInterface $passwordHasher, AppUser $user)
  57.     {
  58.         // ... e.g. get the user data from a registration form
  59.         
  60.         
  61.         return $this->render('registration/confirmed.html.twig', [
  62.         
  63.             'user' => $user
  64.         ]);
  65.     }
  66.     
  67. }