<?php
namespace App\Controller;
use App\Entity\Image;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\TextFr;
use App\Entity\TextEs;
use App\Entity\TextPt;
class HomeController extends AbstractController
{
#[Route('/')]
public function indexNoLocale(): Response
{
return $this->redirectToRoute('home', ['_locale' => 'fr']);
}
#[Route('/{_locale<%app.supported_locales%>}/', name: 'home')]
public function index(Request $request, EntityManagerInterface $entityManager): Response
{
$local = $request->getLocale(); // get la locale
$datasImages = $entityManager->getRepository(Image::class)->findBy(['page' => 'accueil']);
$images = [];
foreach ($datasImages as $img) {
$images[$img->getName()] = $img;
}
switch ($local) {
case "fr":
$datas = $entityManager->getRepository(TextFr::class)->findBy(['page' => 'accueil']);
break;
case "es":
$datas = $entityManager->getRepository(TextEs::class)->findBy(['page' => 'accueil']);
break;
case "pt":
$datas = $entityManager->getRepository(TextPt::class)->findBy(['page' => 'accueil']);
break;
default:
$datas = null;
break;
}
$texts = [];
foreach ($datas as $text) {
$texts[$text->getName()] = $text;
}
return $this->render('accueil/' . $local . '/index.html.twig', [
'texts' => $texts,
'images' => $images
]);
}
}