<?php
namespace App\Controller;
use App\Entity\Abonne;
use App\Form\AbonneType;
use App\Entity\BlogArticle;
use App\Form\BlogArticleType;
use App\Repository\LabelRepository;
use App\Repository\AbonneRepository;
use App\Repository\SettingRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\BlogArticleRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/blog")
*/
class BlogArticleController extends AbstractController
{
/**
* @Route("/", name="app_blog_article_index", methods={"GET"})
*/
public function index(
BlogArticleRepository $blogArticleRepository,
SettingRepository $settingRepository,
LabelRepository $labelRepository,
EntityManagerInterface $em,
Request $request,
PaginatorInterface $paginator
): Response {
$dql = "SELECT a FROM App:BlogArticle a WHERE a.isEnabled = true ORDER BY a.id DESC";
$query = $em->createQuery($dql);
$articles = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
3 /*limit per page*/
);
return $this->render('blog_article/index.html.twig', [
'articles' => $articles,
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'pageTitle' => 'blog'
]);
}
/**
* @Route("/{slug}", name="app_blog_article_show", methods={"GET"})
*/
public function show(
BlogArticle $blogArticle,
SettingRepository $settingRepository,
LabelRepository $labelRepository,
Request $request,
AbonneRepository $abonneRepository
): Response {
$abonne = new Abonne();
$form = $this->createForm(AbonneType::class, $abonne);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$abonneRepository->add($abonne, true);
$this->addFlash('success', 'Votre inscription à notre Newsletter a bien été prise en compte.');
return $this->redirectToRoute('app_blog_article_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('blog_article/show.html.twig', [
'article' => $blogArticle,
'settings' => $settingRepository->findAll(),
'labels' => $labelRepository->findAll(),
'form' => $form->createView(),
'pageTitle' => 'blog'
]);
}
}