所以我从 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()
;
}
但是,我不确定如何实际使用操作本身。我想在两个地方删除,但我不确定这两个地方的正确方法:
- 在编辑操作中删除 - 如何向表单生成器添加删除按钮?我是否必须在 Twig 模板本身中执行此操作?
- 在索引中删除 - 我知道如何调用操作(例如
<a href="{{ path('category_edit', {'id': cat.id}) }}" class="btn btn-default">Edit</a>
),但这不适用于删除操作。
我试过查看 Symfony demo application ,但我仍然没有完全理解删除操作是如何工作的——而且我在文档中找不到任何内容。
有人可以简要解释一下 1 和 2 的删除操作是如何工作的吗?
最佳答案
Symfony CRUD
中的删除脚本与表单提交一起使用。因此,您必须呈现一个表单来显示删除按钮。
您可能会犹豫是否为列表中的每一项呈现一个表单。从内部编辑表单嵌入也不方便。
在 Bootstrap 模式(删除确认框)和 ajax 提交的帮助下,我遇到了这个解决方案:
- 使您的
deleteAction
支持GET
,以便它可以在必要时呈现表单。 - 无论您在哪里需要删除链接,都将其作为指向模态的链接,模态又会加载
deleteAction
Controller 以在模态主体内呈现表单。您也可以在模态中显示确认消息。 - 提交模式表单后,相应地处理您的
deleteAction
脚本,然后重定向。
编辑:为Controller
和Template
添加脚本
/**
* @Route("category/delete/{id}", name="category_delete")
* @Method({"GET", "DELETE"})
*/
public function deleteAction(Request $request, $id)
{
/*
* Check Permission.
*/
$response = array(
'success' => true,
'message' => '',
'html' => '',
);
$repository = $this->getDoctrine()->getRepository('AppBundle:Category');
$category = $repository->find($id);
$form = $this->createDeleteForm($category);
if ($request->getMethod() == 'DELETE') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($category);
$em->flush();
// Get response ready as per your need.
$response['success'] = true;
$response['message'] = 'Deleted Successfully!';
} else {
$response['success'] = false;
$response['message'] = 'Sorry category could not be deleted!';
}
return new JsonResponse($response);
// In case you want to redirect.
// $this->addFlash('notice', 'Deleted Successfully!');
// return $this->redirectToRoute('category_index');
}
$render = $this->render(':category:delete_confirm.html.twig', array(
'delete_form' => $form->createView(),
'category' => $category,
));
$response['html'] = $render->getContent();
return new JsonResponse($response);
}
返回模态的 Twig HTML 代码(我正在使用 UIKIT 模态)
{{ form_start(delete_form) }}
<div class="uk-modal-header">
<h3 class="uk-modal-title">Delete this Category?</h3>
</div>
<p>
Are you sure you want to delete '{{ category.name }}'?<br/>
This cannot be undone.
</p>
<div class="uk-modal-footer uk-text-right">
<div>
<button type="submit" class="md-btn md-btn-danger" title="Click to proceed!">
<i class="uk-icon-trash"></i> Delete
</button>
<button type="button" class="md-btn md-btn-warning uk-modal-close">Cancel</button>
</div>
</div>
{{ form_end(delete_form) }}
希望这对您有所帮助!
关于php - Symfony 3 在 Twig 中使用删除操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733249/