<?php
namespace App\Controller\Front;
use App\Entity\{Article, ArticleCategory};
use App\Service\AccessClientService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BlogController extends AbstractController
{
/**
* @Route("/blog", name="app_front_blog")
*/
public function articles(AccessClientService $accessClientService, Request $request, EntityManagerInterface $entityManager, PaginatorInterface $paginator): Response
{
$accessClientService->handleAccessControl();
$categories = $entityManager->getRepository(ArticleCategory::class)->findBy([], ['id' => 'DESC']);
$lastArticles = $entityManager->getRepository(Article::class)->findBy(['status' => true], ['id' => 'DESC'], 10, 0);
$query = $entityManager->getRepository(Article::class)->findBy(['status' => true], ['id' => 'DESC']);
$pagination = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
12
);
return $this->render('front/blog/articles.html.twig', [
'categories' => $categories,
'lastArticles' => $lastArticles,
'articles' => $pagination
]);
}
/**
* @Route("/blog/{slug}", name="app_front_blog_by_category")
*/
public function by_category(ArticleCategory $category, Request $request, EntityManagerInterface $entityManager, PaginatorInterface $paginator)
{
$categories = $entityManager->getRepository(ArticleCategory::class)->findBy([], ['id' => 'DESC']);
$query = $entityManager->getRepository(Article::class)->getArticlesByCategory($category);
$pagination = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
12
);
return $this->render('front/blog/by_category.html.twig', [
'category' => $category,
'categories' => $categories,
'articles' => $pagination
]);
}
/**
* @Route("/blog/{cat_slug}/{slug}", name="app_front_blog_read")
*/
public function read($cat_slug, Article $article, EntityManagerInterface $entityManager)
{
if($article->getCategory()->getSlug() != $cat_slug)
throw new NotFoundHttpException('Cette catégorie est introuvable !');
$categories = $entityManager->getRepository(ArticleCategory::class)->findBy([], ['id' => 'DESC']);
$lastArticles = $entityManager->getRepository(Article::class)->getLastArticles($article->getId(), $article->getCategory(), 10);
return $this->render('front/blog/read.html.twig', [
'article' => $article,
'categories' => $categories,
'lastArticles' => $lastArticles
]);
}
}