Validate route parameters

Last updated on
12 March 2024

This documentation needs work. See "Help improve this page" in the sidebar.

Drupal 8's routes placeholder values (i.e., parameters passed in the URL) may be validated using the routing system. Placeholder values could be of type string, integer, or any pattern that can be validated with a regular expression. If validation of the parameters fails, then a page not found is returned.

We can define constraints for the values of the placeholders as regular expressions.
For example in example.routing.yml:
{name} placeholder should string and contains only uppercase and lowercase alphabet.

example.user
  path: '/example/{name}'
  defaults:
    _controller: '\Drupal\example\Controller\ExampleController::content'
  requirements:
    _permission: 'access content'
    name: '[a-zA-Z]+'

In the case the validation fails, by default the return will be a 404 page.

Help improve this page

Page status: Needs work

You can: