src/Controller/SecurityController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use App\Entity\User;
  9. use App\Form\RegistrationFormType;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. class SecurityController  extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/login", name="app_login")
  15.      */
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         // get the login error if there is one
  19.         $error = $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername = $authenticationUtils->getLastUsername();
  22.         return $this->render('admin/security/login.html.twig', [
  23.             'last_username' => $lastUsername,
  24.             'error'         => $error,
  25.         ]);
  26.     
  27.     }
  28.     /**
  29.      * @Route("/logout", name="app_logout", methods={"GET"})
  30.      */
  31.     public function logout()
  32.     {   
  33.         // controller can be blank: it will never be called!
  34.         throw new \Exception('Don\'t forget to activate logout in security.yaml');
  35.     }
  36.     /**
  37.      * @Route("/register", name="app_register")
  38.      */
  39.     public function register(Request $request, UserPasswordEncoderInterface $userPasswordEncoderInterface): Response
  40.     {
  41.         $user = new User();
  42.         $form = $this->createForm(RegistrationFormType::class, $user);
  43.         $form->handleRequest($request);
  44.         // dump($form);
  45.         if ($form->isSubmitted() && $form->isValid()) {
  46.             // encode the plain password
  47.             $user->setPassword(
  48.             $userPasswordEncoderInterface->encodePassword(
  49.                     $user,
  50.                     $form->get('plainPassword')->getData()
  51.                 )
  52.             );
  53.             $user->setIsActif(true);
  54.             $user->setRoles(['ROLE_USER']);
  55.             $entityManager = $this->getDoctrine()->getManager();
  56.             $entityManager->persist($user);
  57.             $entityManager->flush();
  58.             // do anything else you need here, like send an email
  59.             return $this->redirectToRoute('mon_compte');
  60.         }
  61.         return $this->render('security/register.html.twig', [
  62.             'form' => $form->createView(),
  63.         ]);
  64.     }
  65.  }