NewsController.php 1.17 KB
Newer Older
1 2 3 4 5 6
<?php

namespace App\Controller;

use App\Entity\News;
use Doctrine\ORM\EntityManagerInterface;
7
use Knp\Component\Pager\PaginatorInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10 11 12 13 14
use Symfony\Component\Routing\Annotation\Route;

class NewsController extends AbstractController
{
    private $em;
15
    private $paginator;
16

17
    public function __construct(EntityManagerInterface $em, PaginatorInterface $paginator)
18 19
    {
        $this->em = $em;
20
        $this->paginator = $paginator;
21 22 23 24 25
    }

    /**
     * @Route("/news", name="news")
     */
26
    public function listeNewsAction(Request $request)
27
    {
28 29 30 31 32
        $pagination = $this->paginator->paginate(
            $this->em->getRepository(News::class)->findLatest(),
            $request->query->getInt('page', 1),
            5
        );
33
        return $this->render('news/liste.html.twig', array(
34
            'news' => $pagination,
35 36 37 38
        ));
    }

    /**
39
     * @Route("/news/{slug}", name="show_news")
40 41 42 43 44 45 46 47
     */
    public function showNewsAction(News $news)
    {
        return $this->render('news/show.html.twig', array(
            'news' => $news
        ));
    }
}