-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Allow optional route parameters to not be at end of route #31166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Ref. #5424 |
Right now, you can achieve this using two routes, right? |
You're definitely right for a few routes this can be achieved with multiple, however the use case that found me digging in to the matcher was a locale prefix. Basically I wanted to be able to match routes with and without the locale so I could properly redirect to the locale prefixed versions based on the system / current user locale. For this case the prefix is a route group around the entire web routes so duplication wasn't really the best option. I think the changes shouldn't add too much complexity to the matcher as it should only be targetting the first segment if it's optional. I'll put together a PR and some tests |
For such use cases, I see two solutions:
|
No worries, I can look at setting up a listener prior to the router to force a locale prefix instead. |
@jrbarnard note that if you want to give it a try, PRs are always welcome. |
@nicolas-grekas Thanks, I've been meaning to but haven't had an opportunity yet, I'll put something together this week. |
Closed PR |
Description
Currently optional parameters are restricted to being at the end of routes, docs here: https://symfony.com/doc/current/routing/optional_placeholders.html
However looking into the RouteCompiler and the generated regex it's relatively trivial to generate regex which would allow for optional parameters before the end of the route to work and match correctly.
E.g {optionalparameter}/world
/hello/world and /world would match.
The use case was actually found when working on a laravel project and getting locale prefixes to work optionally (as I'm using some middleware to detect if the locale is missing and then forcing a redirect to the locale specific route if none is present, however this currently doesn't work.
I could override the laravel route compiler potentially however I figured this would be a useful core feature of the symfony routing component.
Example
Usage would be identical to currently, just with the possibility of having optional parameters before the end of the route.
Specific info
Example current regex being compiled:
Regex for this would be changed to:
I'm happy to do a PR for this, I just wanted to guage the interest in this feature
The text was updated successfully, but these errors were encountered: