src/Controller/RegistrationController.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\UserType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. class RegistrationController extends AbstractController
  10. {
  11.     private $passwordEncoder;
  12.     public function __construct(UserPasswordEncoderInterface $passwordEncoder)
  13.     {
  14.         $this->passwordEncoder $passwordEncoder;
  15.     }
  16.     /**
  17.      * @Route("/registration", name="registration")
  18.      */
  19.     public function index(Request $request)
  20.     {
  21.         $user = new User();
  22.         $form $this->createForm(UserType::class, $user);
  23.         $form->handleRequest($request);
  24.         if ($form->isSubmitted() && $form->isValid()) {
  25.             // Encode the new users password
  26.             $user->setPassword($this->passwordEncoder->encodePassword($user$user->getPassword()));
  27.             // Set their role
  28.             $user->setRoles(['ROLE_USER']);
  29.             // Save
  30.             $em $this->getDoctrine()->getManager();
  31.             $em->persist($user);
  32.             $em->flush();
  33.             return $this->redirectToRoute('app_login');
  34.         }
  35.         return $this->render('registration/index.html.twig', [
  36.             'form' => $form->createView(),
  37.         ]);
  38.     }
  39. }