Score:1

Custom module routing issue

cn flag

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

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

On the controller I've the following code

public function build($someVariable)
  {
    echo 'here';
    dump($someVariable);
    exit;

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

Thanks

Kevin avatar
in flag
I don't think routes can begin with a parameter.
cn flag
@Kevin in Symfony you can. not sure in Drupal but should be possible
4uk4 avatar
cn flag
@Paulo, Drupal's own route matching doesn't allow this. For a workaround see https://drupal.stackexchange.com/questions/246950/do-new-routes-require-a-static-first-portion
Jaypan avatar
de flag
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.
cn flag
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
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.