restrict http methods on routes in drupal 8

Good sources for quick overview:

Definition of routes happens in yourmodule.config.yml. The structure of a route definition is documented on d.o., but documentation is incomplete.

Especially I couldn’t find any useful documentaction on how to restrict the route definition to a specific method (in my case GET). The symfony way of using methods: [GET] doesn’t work. Seems that drupal is using a depreciated way of defining the allowed methods. Symfony’s route.php on github (see setMethods)

The solution as today is to put an _method definition under the requirements section in the yaml file:

_method: 'GET'

Note that you have to use a string, even in case of more than one method. Example: ‘GET|POST’.

Change record on d.o. on upgrading to symfony 3.0 in a subsequent minor release.
See also the upgrade notes from the symonfy team at github. According to these docs, the structure of the routing definitcccion will likely change soon.

To setup other HTTP Methods than GET and POST for your route, you can use symfony’s fake method parameter in the query string. I didn’t test if this actually works on a D8 installation yet.


Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting this form, you accept the Mollom privacy policy.