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 $requestUserPasswordHasherInterface $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.         }
  44.         
  45.         return $this->render('registration/register.html.twig', [
  46.         
  47.             'form' => $form->createView()
  48.         ]);
  49.     }
  50. }