简体   繁体   English

删除Symfony2中的数据

[英]Deleting data in Symfony2

删除每一行中的选项

I have delete options for each row like this. 我有像这样的每一行删除选项。 I successfully deleted the data using GET method but How to delete in POST/DELETE method using the same layout? 我已成功使用GET方法删除了数据,但如何使用相同的布局在POST / DELETE方法中删除? Is GET method the safe way to delete? GET方法是删除的安全方法吗?

To use the DELETE method the best way is to use a form with a POST method and then fake the DELETE using a _method field. 要使用DELETE方法,最好的方法是将表单与POST方法一起使用,然后使用_method字段伪造DELETE The form can then set the CSS of the form to be display: inline; 然后,表单可以将表单的CSS设置为display: inline; or display: inline-block; display: inline-block; .

Your "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>

Your css 你的CSS

form.delete-button-form {
    display: inline-block;
}

You can see a fiddle of this layout here 您可以在这里看到这种布局的小提琴

You will also need to set the config to use the http method override which the docs cover better than I could. 您还需要将配置设置为使用http方法覆盖,文档会比我能更好地覆盖它。 Taken from 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方法是删除的安全方法吗?

I don't think is safer to use GET , POST , or DELETE . 我认为使用GETPOSTDELETE并不安全。 Whatever method you use, you still have to add rules in the firewall or in the controller to ensure that an user has the right to delete something. 无论使用哪种方法,都仍然必须在防火墙或控制器中添加规则,以确保用户有权删除某些内容。

You can declare a specific route for deleting an instance of your entity, without using any form: 您可以声明一种删除实体实例的特定路线,而无需使用任何形式:

/**
 * @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'));
}

Then create a button for each row of the table: 然后为表格的每一行创建一个按钮:

<a href="{{ path('something_delete', {'id': something.id}) }}">
    <button type="button">Delete</button></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM