فهرست منبع

Ajout du controller de recherche par tag

Sangfroid 1 ماه پیش
والد
کامیت
ac943e0dbe
4فایلهای تغییر یافته به همراه52 افزوده شده و 2 حذف شده
  1. 24 0
      src/Controller/SearchController.php
  2. 20 0
      src/Repository/ArticleRepository.php
  3. 7 1
      templates/index/index.html.twig
  4. 1 1
      templates/view/index.html.twig

+ 24 - 0
src/Controller/SearchController.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Controller;
+
+use App\Repository\ArticleRepository;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Attribute\Route;
+
+class SearchController extends AbstractController
+{
+    #[Route('/tag/{tag}', name: 'app_search')]
+    public function index(
+        string $tag,
+        ArticleRepository $articleRepository
+    ): Response
+    {
+        $articles = $articleRepository->findByTag($tag);
+        return $this->render('index/index.html.twig', [
+            'tag' => $tag,
+            'articles' => $articles
+        ]);
+    }
+}

+ 20 - 0
src/Repository/ArticleRepository.php

@@ -51,4 +51,24 @@ class ArticleRepository extends ServiceEntityRepository
             ->getOneOrNullResult()['publicationDate'] ?? null
         ;
     }
+
+    public function findByTag(string $tag, bool $onlyPublished = true): array
+    {
+        $qb = $this->createQueryBuilder('a');
+
+        if ($onlyPublished) {
+            $qb->andWhere('a.state = :published')
+                ->setParameter('published', 'published')
+            ;
+        }
+
+        return $qb
+            ->innerJoin('a.tags', 't')
+            ->andWhere($qb->expr()->like('t.name', ':tag'))
+            ->setParameter('tag', '%'.$tag.'%')
+            ->orderBy('a.publicationDate', 'DESC')
+            ->getQuery()
+            ->getResult()
+        ;
+    }
 }

+ 7 - 1
templates/index/index.html.twig

@@ -4,6 +4,12 @@
 
 {% block body %}
 
+{% if tag is defined %}
+<section>
+    <h1>Etiquette : {{ tag }}</h1>
+</section>
+{% endif %}
+
 <div>
     <section id="section-articles">
         {% for article in articles %}
@@ -13,7 +19,7 @@
                     <p class="article-by">{{ article.publicationDate | format_date('long') }} - {{ article.author }}</p>
                     <p class="article-by">
                         {% for tag in article.tags %}
-                            <span class="tag">{{ tag }}</span>
+                            <a href="{{ path('app_search', {'tag': tag.name}) }}"><span class="tag">{{ tag }}</span></a>
                             {% if not loop.last %} {% endif %}
                         {% endfor %}
                     </p>

+ 1 - 1
templates/view/index.html.twig

@@ -18,7 +18,7 @@
             <p class="article-by">{{ article.publicationDate | format_date('long')}} - {{ article.author }}</p>
             <p class="article-by">
                 {% for tag in article.tags %}
-                    <span class="tag">{{ tag }}</span>
+                    <a href="{{ path('app_search', {'tag': tag.name}) }}"><span class="tag">{{ tag }}</span></a>
                     {% if not loop.last %} {% endif %}
                 {% endfor %}
             </p>