在 Symfony2 中删除数据
Deleting data in Symfony2
我有像这样的每一行的删除选项。我使用 GET 方法成功删除了数据,但是如何使用相同的布局在 POST/DELETE 方法中删除? GET 方法是安全的删除方法吗?
要使用 DELETE
方法,最好的方法是使用带有 POST
方法的表单,然后使用 _method
字段伪造 DELETE
。然后表单可以将表单的 CSS 设置为 display: inline;
或 display: inline-block;
.
你的"delete button"
<form action="{{ path('your_path') }}" method="POST" class="delete-button-form>
<input name="_method" value="DELETE" type="hidden">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
你的css
form.delete-button-form {
display: inline-block;
}
你可以看到一个fiddle这个布局here
您还需要设置配置以使用 http 方法覆盖,文档比我能介绍的更好。 摘自here
The _method functionality shown here is disabled by default in Symfony 2.2 and
enabled by default in Symfony 2.3. To control it in Symfony 2.2, you must call
Request::enableHttpMethodParameterOverride before you handle the request (e.g.
in your front controller). In Symfony 2.3, use the http_method_override option.
Is GET method the safe way to delete?
我认为使用 GET
、POST
或 DELETE
并不安全。无论你使用什么方法,你仍然必须在防火墙或控制器中添加规则以确保用户有权删除某些内容。
您可以声明用于删除实体实例的特定路由,而无需使用任何形式:
/**
* @Route(
* "/something/{id}/delete",
* name="something_delete"
* )
*/
public function somethingRemoveAction($id)
{
$something = $this->getDoctrine()
->getRepository('ACMEBundle:Something')
->find($id);
$em = $this->getDoctrine()->getManager();
$em->remove($something);
$em->flush();
// Suggestion: add a message in the flashbag
// Redirect to the table page
return $this->redirect($this->generateUrl('something_list'));
}
然后为table的每一行创建一个按钮:
<a href="{{ path('something_delete', {'id': something.id}) }}">
<button type="button">Delete</button></a>
我有像这样的每一行的删除选项。我使用 GET 方法成功删除了数据,但是如何使用相同的布局在 POST/DELETE 方法中删除? GET 方法是安全的删除方法吗?
要使用 DELETE
方法,最好的方法是使用带有 POST
方法的表单,然后使用 _method
字段伪造 DELETE
。然后表单可以将表单的 CSS 设置为 display: inline;
或 display: inline-block;
.
你的"delete button"
<form action="{{ path('your_path') }}" method="POST" class="delete-button-form>
<input name="_method" value="DELETE" type="hidden">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
你的css
form.delete-button-form {
display: inline-block;
}
你可以看到一个fiddle这个布局here
您还需要设置配置以使用 http 方法覆盖,文档比我能介绍的更好。 摘自here
The _method functionality shown here is disabled by default in Symfony 2.2 and
enabled by default in Symfony 2.3. To control it in Symfony 2.2, you must call
Request::enableHttpMethodParameterOverride before you handle the request (e.g.
in your front controller). In Symfony 2.3, use the http_method_override option.
Is GET method the safe way to delete?
我认为使用 GET
、POST
或 DELETE
并不安全。无论你使用什么方法,你仍然必须在防火墙或控制器中添加规则以确保用户有权删除某些内容。
您可以声明用于删除实体实例的特定路由,而无需使用任何形式:
/**
* @Route(
* "/something/{id}/delete",
* name="something_delete"
* )
*/
public function somethingRemoveAction($id)
{
$something = $this->getDoctrine()
->getRepository('ACMEBundle:Something')
->find($id);
$em = $this->getDoctrine()->getManager();
$em->remove($something);
$em->flush();
// Suggestion: add a message in the flashbag
// Redirect to the table page
return $this->redirect($this->generateUrl('something_list'));
}
然后为table的每一行创建一个按钮:
<a href="{{ path('something_delete', {'id': something.id}) }}">
<button type="button">Delete</button></a>