src/Controller/BlogArticleController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Abonne;
  4. use App\Form\AbonneType;
  5. use App\Entity\BlogArticle;
  6. use App\Form\BlogArticleType;
  7. use App\Repository\LabelRepository;
  8. use App\Repository\AbonneRepository;
  9. use App\Repository\SettingRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use App\Repository\BlogArticleRepository;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. /**
  18.  * @Route("/blog")
  19.  */
  20. class BlogArticleController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="app_blog_article_index", methods={"GET"})
  24.      */
  25.     public function index(
  26.         BlogArticleRepository $blogArticleRepository,
  27.         SettingRepository $settingRepository,
  28.         LabelRepository $labelRepository,
  29.         EntityManagerInterface $em,
  30.         Request $request,
  31.         PaginatorInterface $paginator
  32.     ): Response {
  33.         $dql "SELECT a FROM App:BlogArticle a WHERE a.isEnabled = true ORDER BY a.id DESC";
  34.         $query $em->createQuery($dql);
  35.         $articles $paginator->paginate(
  36.             $query/* query NOT result */
  37.             $request->query->getInt('page'1), /*page number*/
  38.             /*limit per page*/
  39.         );
  40.         return $this->render('blog_article/index.html.twig', [
  41.             'articles' => $articles,
  42.             'settings' => $settingRepository->findAll(),
  43.             'labels' => $labelRepository->findAll(),
  44.             'pageTitle' => 'blog'
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/{slug}", name="app_blog_article_show", methods={"GET"})
  49.      */
  50.     public function show(
  51.         BlogArticle $blogArticle,
  52.         SettingRepository $settingRepository,
  53.         LabelRepository $labelRepository,
  54.         Request $request,
  55.         AbonneRepository $abonneRepository
  56.     ): Response {
  57.         $abonne = new Abonne();
  58.         $form $this->createForm(AbonneType::class, $abonne);
  59.         $form->handleRequest($request);
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             $abonneRepository->add($abonnetrue);
  62.             $this->addFlash('success''Votre inscription à notre Newsletter a bien été prise en compte.');
  63.             return $this->redirectToRoute('app_blog_article_index', [], Response::HTTP_SEE_OTHER);
  64.         }
  65.         return $this->render('blog_article/show.html.twig', [
  66.             'article' => $blogArticle,
  67.             'settings' => $settingRepository->findAll(),
  68.             'labels' => $labelRepository->findAll(),
  69.             'form' => $form->createView(),
  70.             'pageTitle' => 'blog'
  71.         ]);
  72.     }
  73. }