src/Controller/HomeController.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use App\Entity\Connexion;
  8. use App\Repository\ConnexionRepository;
  9. class HomeController extends AbstractController
  10. {
  11.     /**
  12.      * @Route("/", name="home")
  13.      */
  14.     public function index(): Response
  15.     {
  16.         return $this->render('home/index.html.twig', []);
  17.     }
  18.     
  19.     /**
  20.      * @Route("/enregistreradresseipdeconnexion", name="home.connexion")
  21.      */
  22.     public function connexion(ConnexionRepository $connexionRepositoryEntityManagerInterface $em): Response
  23.     {
  24.         $connexion $connexionRepository->findOneByUser($this->getUser());
  25.         if ($connexion == null) {
  26.             $connexion = new Connexion();
  27.             $connexion->setUser($this->getUser());
  28.         }
  29.         if(!empty($_SERVER['HTTP_CLIENT_IP'])){
  30.             $ip $_SERVER['HTTP_CLIENT_IP'];
  31.         }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  32.             $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
  33.         }else{
  34.             $ip $_SERVER['REMOTE_ADDR'];
  35.         }
  36.         $connexion->setIp($ip);
  37.         $connexion->setDate(new \DateTime('now'));
  38.         
  39.         $em->persist($connexion);
  40.         $em->flush($connexion);
  41.         return $this->redirectToRoute('home');
  42.     }
  43. }