- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
所以我从 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()
;
}
但是,我不确定如何实际使用操作本身。我想在两个地方删除,但我不确定这两个地方的正确方法:
<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/
Twig 使用 {{ }}、{% %}、{# #} 分隔符。 但是如何在 Twig 模板中显示 {{ }} 呢?我不是在谈论 HTML 转义。 我问这个问题是因为我想在我的 Twig 模板中包含一个
为了转换我的法语 html 口音,我尝试通过在 Twig 中执行此操作来更改编码: {{ ddf.description | convert_encoding('UTF-8', 'HTML-ENTIT
我有一个带有输出元素的 .twig 文件的循环。 我需要为每个元素增加一个值。我知道如何在 PHP 中执行此操作,但不清楚如何使用 twig 文档执行此操作。我真的不能在 Controller 中做到
我有一个 xxx.html.twig 文件,它显示一个页面,但是当我想用不同的数据刷新页面并用新数据更新它时,我有一个选择和一个提交按钮。问题是我不知道如何在 Controller 中调用一个 Act
我需要使用 Liquid 来创建模板,但可能只能访问 Twig 模板语言。这两种语言基本上是相同的东西,还是语言上存在差异,导致很难使用 Twig 来替代 Liquid? 最佳答案 Twig 和 Li
number rounding 的 Twig 文档谈论四舍五入小数,但我有一个案例,我想将 19,995 这样的数字四舍五入到 20,000。是否有一种巧妙的方法可以四舍五入到最接近的千位? 最佳答案
我在 2.7 和 3.1 版本中都有 Symfony 项目。 PhpStorm 启用了 Symfony 插件和 Twig 支持插件。 当我在 Symfony 2.7 版本的 PhpStorm 中工作时
我已升级到新版本的 twig/twig 3.x.x。从 twig 版本 3.0 开始,Twig_Extension 已被弃用。所以我接下来要讨论“whiteoctober/breadcrumbs-bu
我在 Twig 中经常有类似的样板代码: {% if (somelongcond) %} {% endif %} Some long content {% if (somelongcond)
我想在我自己的插件中覆盖来自插件的 Twig 模板的 block 。我过去曾覆盖过 native 商店软件模板,但我在使用插件模板时遇到了麻烦。 我正在尝试调整的插件是 FroshProductCom
我用带 Twig 的silex; 我创建自定义 form_div_layout 并将其放在 webroot 中(例如) 我这样注册 TwigService 提供者 $app->register
我不太明白 Twig 中的属性函数是如何工作的。有人可以帮我举个例子吗? 我在 SQL 中有一个名为动态的字段。我可以是例如“field27”,但我不知道号码,号码保存在 radio.id 中。我想做
我想知道是否有一种方法可以在 TWIG 中创建多语句 示例:两个单独的语句... {% set foo:bar %} {% set baz:qux %} 合并成一个语句 {% set foo:
我正在尝试使用 Twig i18n 扩展。 据我所知,我需要的文件在这里: https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig
我正在使用 patternlab 的节点版本用 Twig 作为模板引擎。我正在使用 twig,因为我的代码库是用 twig 编写的 - 所以使用 mustache 不是一个选项。 我简单地尝试包含一个
我有以下代码: {% set pageDescription = item.description|length > 197 ? item.description|striptags|trim|sli
我有以下 Twig 代码: {% for likeditem in user.getItemLikes() %} //iterate over each liked items here {%
反正有没有像这样在 Twig 中重复一个块: {% block title %}{% endblock %} | MyBusiness 为了只声明两个块一次?像那样: {% block title
编辑:2016年12月3日 想学习如何向 Twig 添加自定义扩展(过滤器)吗?请参阅this answer的lxg 您是否只需要找到与ucword等效的嫩枝?请参阅this answer的Javie
我有一个简单的浮点数组。我需要将它显示为逗号分隔的字符串。 {{ arr|join(', ') }} 由于过度的微不足道的准确性,这是一个糟糕的解决方案。 {% for val in arr %}
我是一名优秀的程序员,十分优秀!