Explanation:
I have generated the CRUD of an entity, getting the following default actions:
- indexAction(): lists all entities.
- showAction($id): finds (by ID) and displays an entity.
- deleteAction($id): deletes an entity.
- another actions.
So, I have seen I can only delete an entity within the actions that use the param $id (e.g.: showAction($id) ) but I want to delete an entity inside the indexAction template because I save a step to users.
The deleteAction needs a request, an ID and use the POST method.
I was trying to code something like:
<a href="{{ path('entity_delete', { 'id': entity.id }) }}" class="btn">
<img src="{{ asset('bundles/acme/images/delete.png') }}" ... />
</a>
When I execute the action, I get the following error:
No route found for "GET /acme/something/4/delete": Method Not Allowed (Allow: POST, DELETE)
This response is clear and it's what I expected, so I tried to do something similar but using a form. Something like this:
<form id="formDelete" action="{{ path('entity_delete', { 'id': entity.id }) }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
{{ form_widget(delete_form) }}
<a href="{{ url('entity_delete') }}" class="btn" onclick="document.getElementById('formDelete').submit();">
<img src="{{ asset('bundles/acme/images/delete.png') }}" ... />
</a>
</form>
But the line {{ form_widget(delete_form) }}
is a problem because the indexAction()
hasn't got any parameter and it needs this code:
$deleteForm = $this->createDeleteForm($id);
return $this->render('AcmeBundle:Demo:index.html.twig', array(
'entities' => $entities,
'delete_form' => $deleteForm->createView(),
));
As you can see, the $id param is mandatory for the method createDeleteForm($id)
but I can't get it from indexAction()
.
Question:
What is the best way to solve this issue?