Using mymodule.routing.yml,

  • I can successfully create a custom route at /foo/{bar}/{baz}.

  • I cannot seem to do either of /{bar}/{baz} or /{bar}/foo/{baz}.

It just returns 404.


Is there any way to create a /{bar}/{baz} route (with strict requirements for bar)?

I've read the entirety of the routing API and I can't seem to find any answers there (yet).

  • 1
    "Do new routes in Drupal 8 require a static first portion?" - yes. – user21641 Sep 29 '17 at 5:12

In the routing system you can't do this. What you can do is path aliasing.

Inbound: /{bar}/foo/{baz} -> /foo/{bar}/{baz}

Outbound: /foo/{bar}/{baz} -> /{bar}/foo/{baz}

You need a pattern to apply a regular expression on the path inbound, so /{bar}/{baz} is only possible if the parameters themselves have a distinctive pattern.

I didn't find a tutorial how to build a pathprocessor, but you can use this example from core for the scaffolding: http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/PathProcessor/PathProcessorFront.php

and register both as tagged service in *.services.yml

    class: Drupal\Core\PathProcessor\PathProcessorFront
      - { name: path_processor_inbound, priority: 200 }
      - { name: path_processor_outbound, priority: 200 }
    arguments: ['@config.factory']
  • Awesome, that even answers the next question I was going to ask! – Darvanen Sep 29 '17 at 7:01

You can see this link: Routing System on Drupal.org

It says this (emphasis added):

Path (required): The URL to the route, with a leading forward slash (e.g., path: '/book'). You can use dynamic properties by including them in curly braces. (e.g., path: '/node/{node}/outline'). These will be passed along as arguments via parameter converters to the controller/form (see below). Note that the first item of the path must not be dynamic. You can also define optional parameters at the end of your path (See 'Optional Parameters' on this page).

I hope this is enough to understand that routes cannot start with a wildcard, it must be a static value!

  • Voting up for the attempt, but screenshots aren't very useful to this community, I suggest a quote instead, this may be why you received a downvote. – Darvanen Sep 29 '17 at 7:03
  • Yep. I've added some emphasis to the bit that really answers the question too. – Darvanen Sep 29 '17 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.