How to use HTTP Methods beyond GET and POST in Routes

Version: 2.0
Edit this page

Warning: You are browsing the documentation for Symfony 2.0, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to use HTTP Methods beyond GET and POST in Routes

The HTTP method of a request is one of the requirements that can be checked when seeing if it matches a route. This is introduced in the routing chapter of the book "Routing" with examples using GET and POST. You can also use other HTTP verbs in this way. For example, if you have a blog post entry then you could use the same URL pattern to show it, make changes to it and delete it by matching on GET, PUT and DELETE.

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
blog_show:
    pattern:  /blog/{slug}
    defaults: { _controller: AcmeDemoBundle:Blog:show }
    requirements:
        _method:  GET

blog_update:
    pattern:  /blog/{slug}
    defaults: { _controller: AcmeDemoBundle:Blog:update }
    requirements:
        _method:  PUT

blog_delete:
    pattern:  /blog/{slug}
    defaults: { _controller: AcmeDemoBundle:Blog:delete }
    requirements:
        _method:  DELETE

Unfortunately, life isn't quite this simple, since most browsers do not support sending PUT and DELETE requests. Fortunately Symfony2 provides you with a simple way of working around this limitation. By including a _method parameter in the query string or parameters of an HTTP request, Symfony2 will use this as the method when matching routes. This can be done easily in forms with a hidden field. Suppose you have a form for editing a blog post:

1
2
3
4
5
<form action="{{ path('blog_update', {'slug': blog.slug}) }}" method="post">
    <input type="hidden" name="_method" value="PUT" />
    {{ form_widget(form) }}
    <input type="submit" value="Update" />
</form>

The submitted request will now match the blog_update route and the updateAction will be used to process the form.

Likewise the delete form could be changed to look like this:

1
2
3
4
5
<form action="{{ path('blog_delete', {'slug': blog.slug}) }}" method="post">
    <input type="hidden" name="_method" value="DELETE" />
    {{ form_widget(delete_form) }}
    <input type="submit" value="Delete" />
</form>

It will then match the blog_delete route.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.