src/Controller/BlogController.php line 62

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Article;
  4. use App\Entity\Category;
  5. use App\Entity\Comment;
  6. use App\Entity\Tag;
  7. use App\Form\ArticleType;
  8. use App\Form\CommentType;
  9. use App\Repository\ArticleRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Knp\Component\Pager\PaginatorInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\HttpFoundation\File\UploadedFile;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. class BlogController extends AbstractController
  20. {
  21.     /**
  22.      * @var ManagerRegistry
  23.      */
  24.     private $doctrine;
  25.     public function __construct(ManagerRegistry $doctrine)
  26.     {
  27.         $this->doctrine $doctrine;
  28.     }
  29.     
  30.     // Liste des articles sur la partie publique
  31.     /**
  32.      * @Route("/blog", name="blog")
  33.      */
  34.     public function index(ArticleRepository $repoRequest $requestPaginatorInterface $paginator)
  35.     {
  36.         //$donnees = $repo->findAll();
  37.         $donnees $repo->findArticlesPublished();
  38.         $articles $paginator->paginate(
  39.             $donnees// on passe les données
  40.             $request->query->getInt('page'1), // N° de la page en cours, 1 par défaut
  41.             // nombre d'éléments par page
  42.         );
  43.         return $this->render('blog/index.html.twig', [
  44.             'controller_name' => 'BlogController',
  45.             'articles' => $articles,
  46.         ]);
  47.     }
  48.     // Page d'accueil publique
  49.     /**
  50.      * @Route("/", name="home")
  51.      */
  52.     public function home()
  53.     {
  54.         $arrayArticles = [];
  55.         $categories $this->doctrine->getRepository(Category::class)->findAll();
  56.         foreach($categories as $category) {
  57.             $res $this->doctrine->getRepository(Article::class)->findByCategory($category, ['position' => 'ASC']);
  58.             $arrayArticles[$category->getSlug()] = $res;
  59.         }
  60.         
  61.         $articles = new ArrayCollection($arrayArticles);
  62.         return $this->render('/home.html.twig', [
  63.             'categories' => $categories,
  64.             'articles' => $articles,
  65.             'title' => "Bienvenue sur la page du Dasboard ACS 2020",
  66.         ]);
  67.     }
  68.     // Vue dans la partie publique qui permet d'afficher un article
  69.     /**
  70.      * @Route("/post/{slug}", name="post_show")
  71.      */
  72.     public function show(Article $articleRequest $requestEntityManagerInterface $manager)
  73.     {
  74.         $comment = new Comment();
  75.         $form $this->createForm(CommentType::class, $comment);
  76.         $articles $this->doctrine->getRepository(Article::class)->findByCategory($article->getCategory());
  77.         $form->handleRequest($request);
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             /*$comment->setCreatedAt(new \DateTime())
  80.                     ->setArticle($article);*/
  81.             $manager->persist($comment);
  82.             $manager->flush();
  83.             return $this->redirectToRoute('post_show', [
  84.                 'id' => $article->getId()
  85.             ]);
  86.         }
  87.         return $this->render('blog/show.html.twig', [
  88.             'article' => $article,
  89.             'articles' => $articles,
  90.             'commentForm' => $form->createView()
  91.         ]);
  92.     }
  93. }