<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Connexion;
use App\Repository\ConnexionRepository;
class HomeController extends AbstractController
{
/**
* @Route("/", name="home")
*/
public function index(): Response
{
return $this->render('home/index.html.twig', []);
}
/**
* @Route("/enregistreradresseipdeconnexion", name="home.connexion")
*/
public function connexion(ConnexionRepository $connexionRepository, EntityManagerInterface $em): Response
{
$connexion = $connexionRepository->findOneByUser($this->getUser());
if ($connexion == null) {
$connexion = new Connexion();
$connexion->setUser($this->getUser());
}
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
$connexion->setIp($ip);
$connexion->setDate(new \DateTime('now'));
$em->persist($connexion);
$em->flush($connexion);
return $this->redirectToRoute('home');
}
}