的Symfony 3用嫩枝
问题描述:
删除操作
所以我已经复制从基本删除操作:的Symfony 3用嫩枝
/**
* @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()
;
}
但是,我不知道如何实际使用行为本身。我想要在两个地方执行删除操作,但我不确定正确的方法:
- 在编辑操作中删除 - 如何向表单构建器添加删除按钮?我必须在树枝模板本身中做到这一点吗?
- 在索引中删除 - 我知道如何调用操作(例如
<a href="{{ path('category_edit', {'id': cat.id}) }}" class="btn btn-default">Edit</a>
),但这不适用于删除操作。
我试过看Symfony demo application,但我仍然没有完全掌握删除操作是如何工作的 - 而且我在文档中找不到任何东西。
有人可以提供关于删除操作对1和2的操作方式的简要说明吗?
答
Symfony CRUD
中的删除脚本使用表单提交。所以,你必须渲染一个表单来显示删除按钮。
您可能会为列表中的每个项目呈现一个表单。它也不方便从内部编辑表格嵌入。
有了一点从引导模式(确认框进行删除)和Ajax提交的帮助下,我遇到了这个解决方案:
- 让您
deleteAction
支持GET
,以便它可以在必要时呈现形式。 - 无论你需要删除链接,把它作为链接到模态,反过来将加载
deleteAction
控制器呈现模态身体的一侧的形式。您也可以在模式中输入确认消息。 - 当提交模态表单时,相应地处理您的
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);
}
嫩枝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) }}
希望这有助于!
如果你能分享一些很棒的代码,我对Ajax并不是非常熟悉。 – Darkstarone
为控制器和模板添加脚本。您可以照顾JavaScript来动态呈现模态,并在jQuery的帮助下提交表单。 – Jeet
非常感谢,这是超级信息。我在为删除按钮本身计算html时遇到了一些麻烦,您是否也可以添加它? – Darkstarone