Comment récupérer l'id d'un article ?

Par Devdeb22, il y a 7 ans


Bonjour,

Voila je rencontre un petit problème avec le code suivant pourtant je ne vois pas où est l'érreur

Je develope sous symfony4 dans une page où j'ai une liste d'article je voudrait accéder à un unique article grace à son id qui devrait passer en GET

Sauf que j'ai le message d'erreur suivant qui me dit qu'il me manque le paramètre "id"

An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("id") to generate a URL for route "blog_show".").

in templates\blog\index.html.twig (line 14)

Voici les méthodes du controller

/** * @Route("/blog", name="blog") */ public function index() { $repo = $this->getDoctrine()->getRepository(Article::class); $articles = $repo->findAll(); return $this->render('blog/index.html.twig', [ 'controller_name' => 'BlogController', 'articles' => $articles ]); } /** *@Route("/blog/{id}", name="blog_show") */ public function show($id){ $repo = $this->getDoctrine()->getRepository(Article::class); $article = $repo->find($id); return $this->render('blog/show.html.twig',[ 'article' =>$article ]); }

Voici la vue de la méthode index()

{% extends 'base.html.twig' %} {% block title %}Articles{% endblock %} {% block body %} <section class="articles"> {% for article in articles %} <article> <h2>{{article.title}}</h2> <div class="metadata"> Ecrit le {{article.createdAt | date('d/m/Y')}} à {{article.createdAt | date('H:i')}}</div> <div class="content"> <img src="{{article.image}}" alt=""> {{ article.content | raw }} <a href="{{path('blog_show')}}" class="btn btn-primary">Lire la suite</a> </div> </article> {% endfor %} </section> {% endblock %}

Et voici la vue de la méthode show()

{% extends 'base.html.twig' %} {% block title %}Article{% endblock %} {% block body %} <section class="articles"> <article> <h2>{{article.title}}</h2> <div class="metadata"> Ecrit le {{article.createdAt | date('d/m/Y')}} à {{article.createdAt | date('H:i')}}</div> <div class="content"> <img src="{{article.image}}"> {{article.content |raw}} </div> </article> </section> {% endblock %}

4 réponses

Lartak, il y a 7 ans

Bonjour.
Lorsque tu crées un lien pour voir un article, qui doit par conséquent diriger vers la vue de ta méthode show, il te faut définir le paramêtre id, soit :

<a href="{{path('blog_show', {id: article.id})}}" class="btn btn-primary">Lire la suite</a>
Devdeb22, il y a 7 ans

Un grand merci Lartak

Effectivement la foncton path prend 2 paramètres, il faudra juste penser à mettre les côtes pour 'id' :

Nicolas Albert, il y a 6 ans

Merci cela fonctionne

Digivia, il y a 6 ans

Petit complément d'info, tu n'es pas obligé de rechercher toi-même l'article, Symfony peut le faire lui-même. Par exemple :

/** *@Route("/blog/{id}", name="blog_show") */ public function show(Article $article) { return $this->render('blog/show.html.twig', ['article' =>$article]); }

propose le même résultat que ton code :

/** *@Route("/blog/{id}", name="blog_show") */ public function show($id){ $repo = $this->getDoctrine()->getRepository(Article::class); $article = $repo->find($id); return $this->render('blog/show.html.twig',[ 'article' =>$article ]); }