src/Controller/SecurityController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. class SecurityController extends AbstractController
  11. {
  12.     private \Doctrine\Persistence\ManagerRegistry $managerRegistry;
  13.     public function __construct(\Doctrine\Persistence\ManagerRegistry $managerRegistry)
  14.     {
  15.         $this->managerRegistry $managerRegistry;
  16.     }
  17.     /**
  18.      * @Route("/login", name="app_login")
  19.      */
  20.     public function login(AuthenticationUtils $authenticationUtils): Response
  21.     {
  22.         // if ($this->getUser()) {
  23.         //     return $this->redirectToRoute('target_path');
  24.         // }
  25.         // get the login error if there is one
  26.         $error $authenticationUtils->getLastAuthenticationError();
  27.         // last username entered by the user
  28.         $lastUsername $authenticationUtils->getLastUsername();
  29.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  30.     }
  31.     /**
  32.      * @Route("/register", name="app_register")
  33.      */
  34.     public function register(Request $request\Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $passwordEncoder): Response
  35.     {
  36.         exit('access denied');
  37.         $user = new User();
  38.         $form $this->createForm(RegistrationFormType::class, $user);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted() && $form->isValid()) {
  41.             // encode the plain password
  42.             $user->setPassword(
  43.                 $passwordEncoder->encodePassword(
  44.                     $user,
  45.                     $form->get('plainPassword')->getData()
  46.                 )
  47.             );
  48.             $entityManager $this->managerRegistry->getManager();
  49.             $entityManager->persist($user);
  50.             $entityManager->flush();
  51.             // do anything else you need here, like send an email
  52.             return $this->redirectToRoute('app_login');
  53.         }
  54.         return $this->render('security/register.html.twig', [
  55.             'registrationForm' => $form->createView(),
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route("/logout", name="app_logout")
  60.      *
  61.      * @return never
  62.      */
  63.     public function logout(): Response
  64.     {
  65.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  66.     }
  67. }