Validate route parameters
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
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion