<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\News;
use App\Entity\NewsInterface;
use App\Extension\NewsExtension;
use App\Repository\NewsRepositoryInterface;
use Doctrine\Common\Collections\Collection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
use function Clue\StreamFilter\fun;
class NewsController extends AbstractController
{
private NewsRepositoryInterface $newsRepository;
private Environment $twig;
public function __construct(NewsRepositoryInterface $newsRepository, Environment $twig)
{
$this->newsRepository = $newsRepository;
$this->twig = $twig;
}
/**
* @Route("/news", name="news_index")
*/
public function index(): Response
{
$news = $this->newsRepository
->createVisibleQueryBuilder()
->getQuery()
->getResult();
return $this->render('page/news/index/index.html.twig', [
'posts' => $news,
]);
}
/**
* @Route("/news/rss", name="news_rss")
*/
public function rss() : Response {
$news = $this->newsRepository
->createVisibleQueryBuilder()
->getQuery()
->getResult();
$contents = $this->twig->render('page/news/index/rss.xml.twig', [
'items' => $news,
]);
$response = new Response($contents);
$response->headers->set('Content-Type', 'application/xml');
return $response;
}
/**
* @Route("/news/{news}", name="news_show")
*/
public function show(NewsInterface $news): Response
{
$moreNews = $this->newsRepository
->createVisibleQueryBuilder()
->andWhere('o != :currentNews')
->setParameter('currentNews', $news)
->setMaxResults(3)
->getQuery()
->getResult();
return $this->render('page/news/show/show.html.twig', [
'post' => $news,
'news' => $moreNews,
]);
}
}