Problem/Motivation
According to the Symfony documentation it should be possible to use a "/" (forward slash) in a route path by defining the pattern for a path component to allow any character (instructions here: https://symfony.com/doc/3.4/routing/slash_in_parameter.html)
However, in Drupal this doesn't work.
Steps to reproduce
For example, with the following route definition:
_hello:
path: /hello/{username}
defaults: { _controller: AppBundle:Demo:hello }
requirements:
username: .+
the path '/hello/foo/bar' should work, and the controller should receive $username as 'foo/bar'.
Proposed resolution
Remaining tasks
- File a follow-up to remove Drupal\system\PathProcessor\PathProcessorFiles which is a workaround for this limitation
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Comments
Comment #2
miiimoooComment #4
dawehner@miiimooo
What is the usecase you have for this particular feature?
Comment #5
dawehnerComment #8
DeFr commentedAdding a use case I'm facing: I wanted to clean up Facets Pretty Paths to make it add an optional trailing route parameter in a route subscriber, instead of an InboundPathProcessor ; can't really work currently.
Edit: fixed project link.
Comment #9
DeFr commentedAttaching a proof of concept patch that works on the official Symfony example, setting to needs review for the bot to check if it breaks something.
The real challenge here is to find an appropriate way to check for route parameters that allows a "/" to be in there. Current patch hardcodes ".*" and ".+" which are the examples provided by symfony for this use case and are easy enough to check, but code in the wild might actually provide a more restrictive regexp to validate the argument.
Comment #11
DeFr commentedFrom the correct root this time.
Comment #14
sylvainm commentedComment #15
borisson_I wonder how much of a performance hit this is. I guess that when there are a lot of routes, even without having any of them with unlimited parts, this will slow the entire route generation down quite a bit.
I think this is faster, array_filters are supposed to be.
-->
Even with that, this would need profiling to make sure that this isn't too big of a hit.
Comment #16
zahord commentedHi, updated the patch regarding @borisson_ recommendations
Comment #17
zahord commentedComment #18
DeFr commentedNote: all of this is happening after calling Symfony\Routing\RouteCompiler::compile() ; this in in turn calling RouteCompiler::compilePattern which is already doing a preg_match, putting them in a foreach, and calling getRequirement one by one on the matches. Profiling is always a good thing, but I'd be surprised to see a performance hit caused by this patch in the route compilation phase.
Performance wise, I'd be more concerned about the potential hit at runtime ; this patch introducing an OR in RouteCollection::getRoutesByPath means that potentially, everything trying to retrieve a route collection for a pat could take a hit.
Comment #21
siegristI don't know about the performance, but it seems to work for me!
Comment #22
larowlanStill needs profiling of the performance folks, thanks
Comment #23
chi commentedComment #25
mxr576Comment #26
roderickgadellaabsl commentedApplied the changes in #16 to Drupal core 8.8.x
Comment #27
roderickgadellaabsl commented#26 seems to be corrupted somehow, so this is a reupload. Also set the correct target (8.8.x)
Comment #28
meenakshig commentedComment #31
gaards commentedRe-rolled #27 for 9.2.x
Comment #35
dimilias commentedI opened 2 MRs, 1 for 8.9.x and one for 9.2.x. I wrote tests for this. This is working as expected for the given cases, I fixed one more edge case, but I have issues with it in its core. The two improvements I did were:
$route->setRequirement('some_dummy_text__not_a_route_parameter', '.*'and would still work./some/{param}/{foo}and theparamis set as '.*' andfoois set as '.+', the path '/some/hello/world/blah/blah' might have unexpected results.In general, I am not sure about the solution. I am not sure whether this should be the way to handle it. I think that since Drupal is handling the query parameters as a string divided by '/', a custom case would require to either encode the parameter, or implement something like https://drupal.stackexchange.com/a/225128 in your custom project.
The reason I am thinking of this is that there are many edge cases.
\d{2}/\d{2}/\d{4}(just for the sake of argument) in order to pass a date./some/{param}/{foo}, Thefoo</foo> is not set also to '.*' but to anything else. How do I control that the <code>{param}will not get the whole remaining string and foo will be left blank?{param}is '.*' but I have a sub route'/edit'and so, I don't want it to include a '/' because otherwise I will not be able to access the/some/{param}/edit(this is solvable by my update above).Anyway, I leave it also for the community to decide. The tests are already green. If the community wants to have it as a locked feature, that only in these specific cases works like this, then I am fine with it.
Comment #36
dimilias commentedHowever, to be fair, Symfony is already allowing this as shown at https://symfony.com/doc/4.2/routing/slash_in_parameter.html
So, again, I wrote tests since this exists here. I still think though this is not the best way for the Drupal system.
Comment #40
ranjith_kumar_k_u commentedRerolled #34
Comment #41
spaghettibolognese commentedHi, I tried the patch from #34 but couldn't get it to work with facets_pretty_paths. The `$unlimited_requirements` method uses the stripped path, which doesn't include the default values. Because of this `substr_compare()` fails.
In my usecase changing the `$changed_path` to `$route->getPath();` fixes the problem.
Comment #42
cilefen commentedAlso as yet there is no profiling for performance regressions.
Comment #44
joachim commentedWhat is '$it' meant to mean?
That's not how Symfony works, and presumably we're trying to match what it does. The docs say:
> If the route defines several parameters and you apply this permissive regular expression to all of them, you might get unexpected results. For example, if the route definition is /share/{path}/{token} and both path and token accept /, then token will only get the last part and the rest is matched by path.
-- which means that you can have parameters after the one containing slashes.
Stupid idea which I may come to regret, but instead of making this critical path query more complicated, what if we stored a route that allows a slash in its path with a really big number for number_parts?
Then this wouldn't have a risk of affecting performance.
Comment #45
joachim commentedI've just found that the '.+' requirement actually DOES work for slurping up things into one parameter.
So you can do something like this:
and your controller gets everything in the $parameters function parameter.
Comment #46
DeFr commented@joachim : WRT #45, yes, adding dummy parameters is the workaround currently used in Facets Pretty Path ; the matching RouteSubscriber, adding as much dummies parameters as possible can be found in https://git.drupalcode.org/project/facets_pretty_paths/-/blob/8.x-1.x/sr...
Honestly though, it's not really pretty.
For #44:
#44.1 : Pretty sure that it is coming from @borisson_ comment in this issue (#15, 5 years ago ) and was used as is :) I'd guess it's for $item.
#44.2 : The restriction on the parameter containing a / being the last one wasn't part of the original patch, and was added in #35. I tend to agree that matching Symfony here is best, and as far as I can tell, it should work just fine.
#44.3 : Storing the route with an arbitrarily high number of parts should work. That's what can be achieved today when adding dummy parameters. Given that path is a varchar(255), it doesn't have to be that high to be on the safe side, setting it number_parts to 256 should ensure that it's only matching routes with a wildcard in them. Given that there's an index on (pattern_outline, number_parts), from a performance point of view the OR shouldn't really have an impact, but if that can alleviate performance concerns and get this commited ; that feels like an OK solution :)
Comment #47
nitin shrivastava commentedre-rolled for 10.1.x
Comment #48
akram khanFix CCF #47
Comment #49
DeFr commentedNote: the test that was in #40 is missing from the re-roll in #47
Comment #50
joachim commented> #44.3 : Storing the route with an arbitrarily high number of parts should work. That's what can be achieved today when adding dummy parameters. Given that path is a varchar(255), it doesn't have to be that high to be on the safe side, setting it number_parts to 256 should ensure that it's only matching routes with a wildcard in them. Given that there's an index on (pattern_outline, number_parts), from a performance point of view the OR shouldn't really have an impact, but if that can alleviate performance concerns and get this commited ; that feels like an OK solution :)
Done.
New MR on 10.1.x.
Comment #52
joachim commentedI've just noticed this in the docs at https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/us...
> While Symfony allows for more arbitrary use of slugs, Drupal has stricter requirements. In fact, unlike generic Symfony routes, Drupal requires that a slug occupies a complete path part - the portion between two slashes (or everything after the last slash). If you must pass a parameter containing slashes, apply the same trick as in PathProcessorFiles.
PathProcessorFiles should maybe be changed as a follow-on?
Comment #53
Raviknair45 commentedThe Above patches work only upto 62 url parts after that route will be empty as $ancestors from getCandidateOutlines is having bitwise shift left condition of $end = (1 << $number_parts) - 1; which will skip the execution at https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/lib/Drupal/Core/Routing/RouteProvider.php#L297
Comment #54
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
This could use an issue summary update with proposed solution and remaining tasks. Think it would be a good spot for the profiling to happen also.
Comment #55
joachim commentedUpdated the IS.
Comment #57
biancaradu27 commentedComment #61
isholgueras commentedCore is currently doing for
/system/files/route.It uses an
InboundPathProcessorto get the route of the file, set it to a query parameter and return just the/system/filesto allow the route system to match the route, no matter how many/the file has, because now is in a query parameter.Then, in the controller, it gets the target by accessing the
filequery parameter.Comment #62
prudloff commentedComment #65
prudloff commentedI created a new MR that targets 11.x.
Do we have any doc about profiling this kind of change?
Comment #66
joachim commentedComment #69
prudloff commentedI used xhprof to profile the patch on a standard profile install with cache disabled.
I called
drush cr --xh-linkto generate the reports.I didn't notice a real impact (only ~1000 microseconds of difference in my tests).
The main difference in the stack trace are the closure that is called by array_filter() and the getRequirements() call. And these calls are fast.
Here are screenshots of the xhprof report. I can't upload the .xhprof files because this extension is not allowed.
RouteCompiler::compile() can also be called during runtime if there is a cache miss on a compiled route but I don't think it's worth profiling (it's only one call here and there as opposed to the hundreds of calls during cache rebuild).