0

My question is very easy. How can I specify an http method while using path() helper? I would like to be able to delete a record with it so the DELETE method is necessary. Lets say that my path looks like this:

{{ path("post_delete", {"id": post.id}) }}

where 'post' is twig variable of course. Any idea what I need to put where to achieve my goal?

toblerone_country
  • 577
  • 14
  • 26
Leo
  • 2,061
  • 4
  • 30
  • 58

1 Answers1

1

You cannot. There is no http method in URL. Did you have ever seen POST or GET in your browser address bar?

If you open URL by typing it in address bar in browser, it'll be GET. Only way to use another method in browser is submit a form or using AJAX. You cannot create DELETE hyperlink and even if you can it would be bad idea.

I guest you want delete button. The simplest way to do it in Symfony is creating a form with nothing beside one submit button. It give you possibility to choose HTTP method and give you CSRF protection.

BTW I advise to change question name to "How to create delete button in Symfony?".

Damian Polac
  • 911
  • 9
  • 20
  • 1
    well, for example in RoR, link_to method has option method: which lets you set it to "delete", i thought this case might be similar – Leo Sep 07 '14 at 11:03
  • @Leo I just dug up and found [this question](http://stackoverflow.com/questions/2809200/how-does-rails-3s-data-method-delete-degrade-gracefully) about RoR's link_to method. Like you can see, actually RoR does it by adding inline JavaScript. – Damian Polac Sep 08 '14 at 11:38