Skip to main content
Category:

The routing system that drupal 8 offers is flexible in terms of validation. It is possible to validate your route against some typical regex lines. 

Example 1

{id} placeholder should be a numeric character. 

/path/test/1 will be valid
/path/test/test will be 'page not found'

mymodule.controller:
  path: '/path/{method}/{id}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\mymoduleSaveController::Render'
    _title_callback: '\Drupal\mymodule\Controller\mymoduleSaveController::getTitle'
  requirements:
    _access: 'TRUE'
    id: '\d+'

Example 2 

{name} placeholder should string and contains only uppercase and lowercase alphabet.

/example/test will be valid
/example/100 will be 'page not found'

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

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook