src/Controller/InfoController.php line 21
<?php
namespace App\Controller;
use App\Entity\PageTranslation;
use App\Entity\Paragraph;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class InfoController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $em
){}
#[Route('/{_locale}/info/{slug}', name: 'app_info')]
public function show(string $_locale, string $slug): Response
{
$entityManager = $this->em;
// Fetch the page entity based on the slug
/** @var PageTranslation $pageTranslation */
$pageTranslation = $entityManager->getRepository(PageTranslation::class)->findOneBy(['slug' => $slug]);
if (!$pageTranslation) {
return $this->redirectToRoute("web_homepage");
}
if(!$pageTranslation->getPage()->getIsActive()){
return $this->redirectToRoute("web_homepage");
}
// Get the translations for the page
$translations = $pageTranslation->getPage()->getTranslations();
// Find the translation based on the desired locale
$translation = $translations->filter(function (PageTranslation $translation) use ($_locale) {
return $translation->getLocale() === $_locale;
})->first();
if (!$translation) {
// Find the translation that matches the provided slug, regardless of locale
$matchingTranslation = $translations->filter(function (PageTranslation $translation) use ($slug) {
return $translation->getSlug() === $slug;
})->first();
if ($matchingTranslation) {
// Redirect to the correct URL for the matching translation
$desiredSlug = $matchingTranslation->getSlugByLocale($_locale);
if ($desiredSlug) {
$url = $this->generateUrl('app_info', ['_locale' => $_locale, 'slug' => $desiredSlug]);
return new RedirectResponse($url, 301);
}
}
throw $this->createNotFoundException('Translation not found');
}
$paragraphs = $this->em->getRepository(Paragraph::class)->findBy(["page" => $translation->getPage()]);
// Render the page with the retrieved title and text
return $this->render('info/show.html.twig', [
"page" => $translation,
"paragraphs" => $paragraphs
]);
}
}