src/Controller/SecurityController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. class SecurityController extends AbstractController
  11. {
  12.     #[Route('/admin/login', name: 'app_login')]
  13.     public function login(AuthenticationUtils $authenticationUtils): Response
  14.     {
  15.         // get the login error if there is one
  16.         $error = $authenticationUtils->getLastAuthenticationError();
  17.         // last username entered by the user
  18.         $lastUsername = $authenticationUtils->getLastUsername();
  19.         return $this->render('security/login.html.twig', [
  20.             'error' => $error
  21.         ]);
  22.     }
  23.     #[Route('/admin/logout', name: 'app_logout', methods: ['GET'])]
  24.     public function logout()
  25.     {
  26.         $this->redirectToRoute('app_admin');
  27.     }
  28.     #[Route('/admin/register', name: 'app_register')]
  29.     public function register(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager): Response
  30.     {
  31.         $user = new User();
  32.         $user->setEmail('raoul.andriamalala@gmail.com');
  33.         $user->setNom('Andriamalala');
  34.         $user->setPrenom('Raoul');
  35.         $user->setActif(true);
  36.         $user->setCivilite('Monsieur');
  37.         $plaintextPassword = 'efaniova';
  38.         $hashedPassword = $passwordHasher->hashPassword(
  39.             $user,
  40.             $plaintextPassword
  41.         );
  42.         $user->setPassword($hashedPassword);
  43.         $entityManager->persist($user);
  44.         $entityManager->flush();
  45.         dd($user);
  46.     }
  47. }