src/Controller/SecurityController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. class SecurityController extends AbstractController
  12. {
  13.     #[Route(path'/login'name'app_login')]
  14.     public function login(AuthenticationUtils $authenticationUtils): Response
  15.     {
  16.         if ($this->getUser()) {
  17.             return $this->redirectToRoute('article_index');
  18.         }
  19.         // get the login error if there is one
  20.         $error $authenticationUtils->getLastAuthenticationError();
  21.         // last username entered by the user
  22.         $lastUsername $authenticationUtils->getLastUsername();
  23.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  24.     }
  25.     #[Route(path'/logout'name'app_logout')]
  26.     public function logout(): void
  27.     {
  28.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  29.     }
  30.     #[Route(path:'/registre'name'app_registre')]
  31.     public function registre(UserPasswordHasherInterface $passwordHasherManagerRegistry $doctrine)
  32.     {
  33.         $user = new User();
  34.         $user->setUsername("consulteur");
  35.         $user->setPassword($passwordHasher->hashPassword(
  36.             $user,
  37.             "consulteur@123"
  38.         ));
  39.         $user->setRoles(['ROLE_USER']);
  40.         
  41.         $doctrine->getManager()->persist($user);
  42.         $doctrine->getManager()->flush();
  43.         
  44.         return new Response('good');
  45.     }
  46. }