Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
jrbarnard opened this issue Apr 18, 2019 · 8 comments
Closed

[Routing] Allow optional route parameters to not be at end of route #31166

jrbarnard opened this issue Apr 18, 2019 · 8 comments

Comments

@jrbarnard
Copy link

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:

#^/(?P<optionalparameter>[^/]++)/world$#sDu

Regex for this would be changed to:

#^(?:/(?P<optionalprefix>[^/]++))?/world$#sDu

I'm happy to do a PR for this, I just wanted to guage the interest in this feature

@jrbarnard jrbarnard changed the title [Routing] Allow optional route parameters to not be at end of route Allow optional route parameters to not be at end of route Apr 18, 2019
@jrbarnard jrbarnard changed the title Allow optional route parameters to not be at end of route [Routing] Allow optional route parameters to not be at end of route Apr 18, 2019
@Tobion
Copy link
Contributor

Tobion commented Apr 19, 2019

Ref. #5424

@nicolas-grekas
Copy link
Member

Right now, you can achieve this using two routes, right?
I think the matcher is complex enough: if adding a 2nd route works, we'd better not implement this.
If you think we can achieve this without adding significant complexity, we'd be happy to review a PR.

@jrbarnard
Copy link
Author

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

@nicolas-grekas
Copy link
Member

For such use cases, I see two solutions:

  • either register a listener that runs before RouterListener and extracts the locale before
  • or import the routes twice, with and without a prefix.
    That should do the job in a few lines for a bunch of routes.

@jrbarnard
Copy link
Author

No worries, I can look at setting up a listener prior to the router to force a locale prefix instead.

@nicolas-grekas
Copy link
Member

@jrbarnard note that if you want to give it a try, PRs are always welcome.

@jrbarnard
Copy link
Author

@nicolas-grekas Thanks, I've been meaning to but haven't had an opportunity yet, I'll put something together this week.

@jrbarnard
Copy link
Author

Closed PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants