Enabling arbitrary optional url parameters on Routes (Drupal 8 and later)
Reviewed 230525
AKA Restoring Drupal 7's 'menu tail' functionality
Drupal 7 had this built-in, but the decision was made not to include it in modern Drupal. see: https://www.drupal.org/node/1827544
The following StackExchange explains how to achieve this if you would like to catch all parameters that fall below a particular path using the InboundPathProcessorInterface.
Step-by-step guide
Add a file at mymodule/src/PathProcessor with the following code:
namespace Drupal\mymodule\PathProcessor; use Drupal\Core\PathProcessor\InboundPathProcessorInterface; use Symfony\Component\HttpFoundation\Request; class HelloPathProcessor implements InboundPathProcessorInterface { public function processInbound($path, Request $request) { if (strpos($path, '/hello/') === 0) { $names = preg_replace('|^\/hello\/|', '', $path); $names = str_replace('/',':', $names); return "/hello/$names"; } return $path; } }
Register the class as a service in mymodule.services.yml
This turns everything after '/hello/' in the URL into a colon-separated string so that the dynamic route we registered at '/hello/{names}' gets fired. From there we can split the additional parameters, and use them as needed. This seems like a bit of a kludge but it's handy functionality sometimes and it's been a 'Drupalism' for some time now.
Review History
Who | When | Status |
---|---|---|
|
|
|
Bob | 20230525 | Current |