I've a custom module where I have create the following route:

   path: /{someVariable}/my-module/test
        _controller: '\Drupal\my_module\Controller\myController::build'
        _permission: 'access content'

On the controller I've the following code

public function build($someVariable)
    echo 'here';

When I go to /devel/routes the route is there with the name I see all details of the route

when I call the path function it generates the route as expected, but when I go to the page just throw a 404 response instead give me the expected output.

if I change the path to have the variable in any other position apart of the first part of the url it works as expect

I'm working with Drupal 9.3.2


  • 2
    I don't think routes can begin with a parameter.
    – Kevin
    Jan 19, 2022 at 18:32
  • @Kevin in Symfony you can. not sure in Drupal but should be possible
    – Paulo
    Jan 19, 2022 at 19:04
  • 3
    @Paulo, Drupal's own route matching doesn't allow this. For a workaround see drupal.stackexchange.com/questions/246950/…
    – 4uk4
    Jan 19, 2022 at 19:08
  • Yes, paths cannot start with dynamic variables, the dynamic part of the path must come from the second element onwards. 4k4's link explains how to deal with the problem.
    – Jaypan
    Jan 19, 2022 at 20:15
  • thanks 4k4 an Jaypan. that helped to find the solution. Just one note. on the routing we need to have the variable in the second place and on the inbond and outbound request change the order. that wasn't clear
    – Paulo
    Jan 20, 2022 at 17:47


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.