Răsfoiți Sursa

Passage en mode slug

Sangfroid 2 luni în urmă
părinte
comite
eff7beb46c

+ 3 - 1
src/Controller/ViewController.php

@@ -3,14 +3,16 @@
 namespace App\Controller;
 
 use App\Entity\Article;
+use Symfony\Bridge\Doctrine\Attribute\MapEntity;
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Attribute\Route;
 
 class ViewController extends AbstractController
 {
-    #[Route('/view/{id}', name: 'app_view')]
+    #[Route('/post/{slug}', name: 'app_view')]
     public function index(
+        #[MapEntity(mapping: ['slug' => 'slug'])]
         Article $article
     ): Response
     {

+ 1 - 1
src/Entity/Article.php

@@ -32,7 +32,7 @@ class Article
     #[ORM\Column(length: 64)]
     private ?string $state = null;
 
-    #[ORM\Column(length: 100, unique: true)]
+    #[ORM\Column(length: 100, unique: true, nullable: false)]
     private ?string $slug = null;
 
     public function __construct(User $author)

+ 2 - 2
templates/index/index.html.twig

@@ -8,12 +8,12 @@
     <section id="section-articles">
         {% for article in articles %}
             <article class="article-preview">
-                <h2><a href="{{ path('app_view', {'id': article.id}) }}">{{ article.title}}</a><span class="article-by">{{ article.publicationDate | date('d-m-Y')}} {{ article.author }}</span></h2>
+                <h1><a href="{{ path('app_view', {'slug': article.slug}) }}">{{ article.title}}</a><span class="article-by">{{ article.publicationDate | date('d-m-Y')}} {{ article.author }}</span></h1>
                 <div class="contenu">
                     {{ article.content | markdown }}
                 </div>
             </article>
-            <a href="{{ path('app_view', {'id': article.id}) }}">Voir l'article</a>
+            <a href="{{ path('app_view', {'slug': article.slug}) }}">Voir l'article</a>
             {% if is_granted('edit', article) %}
             <a class="btn btn-blue" href="{{ path('app_article_edit', {'id': article.id}) }}">Modifier cet article</a>
             {% endif %}