ajout d'un lien delete en index
Sur ma page index, j'ai un tableau qui liste les différents commentaires.
Je souhaite ajouter, à côté de chaque commentaire, une action qui permette de supprimer un commentaire, comme ceci:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th class="text-center">#</th>
<th class="text-center">Auteur</th>
<th class="text-center">Contenu</th>
<th class="text-center">Date de création</th>
<th class="text-center">Tutoriel lié</th>
<th class="text-center">Statut</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr class="odd gradeX">
<td>{{ entity.id }}</td>
<td>{{ entity.user }}</td>
<td>{{ entity.content }}</td>
<td class="text-center">{% if entity.createdAt %}{{ entity.createdAt|date('Y-m-d H:i:s') }}{% endif %}</td>
<td>{{ entity.tutorial.title }}</td>
<td>{{ entity.status }}</td>
<td class="table-action">
<a href="{{ path('admin_comment_show', { 'id': entity.id }) }}">
Voir la fiche du commentaire -
</a>
<a href="{{ path('admin_comment_edit', { 'id': entity.id }) }}">
Editer -
</a>
<a href="{{ path('admin_comment_delete', { 'id': entity.id }) }}">
Supprimer -
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table> |
Lorsque je fais ceci, et que je clique sur "supprimer", j'ai cette erreur: No route found for "GET /admin/comment/delete/8": Method Not Allowed (Allow: POST, DELETE)
J'ai lu plusieurs explications, et j'ai bien compris que la suppression devait se faire via un <form>, pour la protection CSRF.
Seulement voilà, je suis perdue...
Actuellement, voici les actions du controller qui gèrent la suppression:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
/**
* Deletes a Comment entity.
* @Route("/delete/{id}", name="admin_comment_delete")
* @Method("DELETE")
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('SiteFrontBundle:Comment')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Comment entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('admin_comment'));
}
/**
* Creates a form to delete a Comment entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm();
} |
L'action index:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
/**
* Lists all Comment entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('SiteFrontBundle:Comment')->findAll();
return $this->render('SiteFrontBundle:Admin/Comment:index.html.twig', array(
'entities' => $entities,
));
} |
Et voici ma route:
Code:
1 2 3 4 5
|
admin_comment_delete:
path: /delete/{id}
defaults: { _controller: "SiteFrontBundle:Admin/Comment:delete" }
requirements: { _method: post|delete } |
Pourriez-vous m'aider svp? D'avance merci