Explanation:
I created a CRUD object, getting the following default actions:
- indexAction (): lists all entities.
- showAction ($ id): Finds (by identifier) ββand displays the object.
- deleteAction ($ id): deletes the object.
- other actions.
So, I saw that I can only delete the entity in actions that use the $ id parameter (for example: showAction ($ id)), but I want to delete the object inside the indexAction template, because I keep the step to the users.
Removal requires a request, identifier, and POST method.
I tried 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 perform an action, I get the following error:
No route found for "GET / acme / something / 4 / delete": method not allowed (Allow: POST, DELETE)
This answer is clear and this is what I expected, so I tried to do something similar, but using the form. Something like that:
<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 indexAction() has no parameter and 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 parameter is required for the createDeleteForm($id) method, but I cannot get it from indexAction() .
Question:
What is the best way to solve this problem?