最佳做法-使用Symfony 2删除链接 [英] Best practices - Delete links with Symfony 2
问题描述
在Symfony 2中,创建链接以删除记录的最佳方法是什么?
In Symfony 2, which is the best way to create a link to delete a record?
我可以定义到/entity/delete
的路由,仅接受DELETE
方法,但是我不知道如何从模板创建DELETE
链接.创建PUT
链接也是如此.
I can define a route to /entity/delete
accepting only a DELETE
method, but I don't know how to create a DELETE
link from a template. The same goes for creating PUT
links.
那么,你怎么办?接受GET
请愿以删除记录吗?有什么方法可以创建DELETE
链接?
So, what do you do? Accept GET
petition to delete a record? Is there any way to create a DELETE
link?
推荐答案
定位器只能触发GET请求.而且由于GET请求可以被缓存,并且在将来(当您实施认真的缓存时)可能根本无法到达您的应用程序代码,因此,使用GET更改任何修改应用程序状态的方法是一种不好的做法.这是我在项目中所做的.有了这两个,您就可以在模板中执行以下操作:
Anchors can fire only GET requests. And since GET requests can be cached and in some future (when you implement serious caching) might not reach your app code at all, it's a bad practice to use GET for changing anything that modifies state of your App. Here is what I do in my projects. With these two, you will be able to just do the following in your templates:
<a href="{{ path('my_entity_destroy') }}" {{ delete_link(myEntity) }}>Delete</a>
工作原理
假设您的实体删除路径为:/my_entity
,方法:[删除]
How it works
Assuming your entity delete path is: /my_entity
, methods: [DELETE]
原理真的很简单. delete_link
扩展方法将在锚点上创建data-attribute
,因此已编译的锚点将如下所示:
Principle is really simple. delete_link
extension method will create data-attribute
on anchor, so compiled anchor will look like this:
<a href="/my_entity" data-delete-link="3964">Delete</a>
然后,当有人点击该链接时,javascript将捕获该点击,创建一个表单并触发data-delete-link
属性中提供的ID的DELETE请求.
Then, when somebody clicks on that link, javascript will catch that click, create a form and fire a DELETE request with id provided in data-delete-link
attribute.
以下是使之成为可能的LinkHelper
Twig扩展名:
Here is what makes it possible, a LinkHelper
Twig extension:
<?php
namespace Me\MyBundle\Twig;
class LinkHelperExtension extends \Twig_Extension
{
public function getFunctions()
{
return [
new \Twig_SimpleFunction('delete_link', [$this, 'fnDeleteLink'], ['is_safe' => ['all']]),
];
}
public function fnDeleteLink($target)
{
if (is_object($target)) {
$target = $target->getId();
}
return "data-delete-link='$target'";
}
public function getName()
{
return 'link_helper';
}
}
在我的基本模板中有一个JavaScript:
And I a JavaScript in my base template:
$(function () {
var createForm = function (action, data) {
var $form = $('<form action="' + action + '" method="POST"></form>');
for (input in data) {
if (data.hasOwnProperty(input)) {
$form.append('<input name="' + input + '" value="' + data[input] + '">');
}
}
return $form;
};
$(document).on('click', 'a[data-delete-link]', function (e) {
e.preventDefault();
var $this = $(this);
var $form = createForm($this.attr('href'), {
id: $this.attr('data-delete-link'),
_method: 'DELETE'
}).hide();
$('body').append($form); // Firefox requires form to be on the page to allow submission
$form.submit();
});
});
限制
它仅适用于具有名为id
的主键的实体.但是,您可以轻松修改它以适合您的需求并支持复合主键等.
Limitations
It only works for entities that have primary key named id
. However, you can pretty easy modify this to suit your needs and support composite primary keys etc.
这篇关于最佳做法-使用Symfony 2删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!