So I've copied the basic delete action from the CRUD generator:
/**
* @Route("category/delete/{id}", name="category_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$repository = $this->getDoctrine()->getRepository('AppBundle:Category');
$category = $repository->find($id);
$form = $this->createDeleteForm($category);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($category);
$em->flush();
}
return $this->redirectToRoute('category_index');
}
/**
*
* @param Category $category
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(Category $category)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('category_delete', array('id' => $category->getId())))
->setMethod('DELETE')
->getForm()
;
}
However, I'm not sure how to actually use the action itself. I want to do deletes in two places, and I'm not sure the correct way to do either:
- Deletes in the edit action - How do I add a delete button to the form builder? I do I have to do it in the twig template itself?
- Deletes in the index - I know how to call actions (e.g.
<a href="{{ path('category_edit', {'id': cat.id}) }}" class="btn btn-default">Edit</a>
), but this doesn't work with the delete action.
I've tried looking at the Symfony demo application, but I still don't fully grasp how the delete action is meant to work - and I can't find anything in the docs.
Can someone provide a brief of explanation of how the delete action works in regard to 1 and 2?