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

Skip to content

Routing - Allow optional parameters throughout the route #31616

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

Conversation

jrbarnard
Copy link

Q A
Branch? master
Bug fix? no
New feature? yes - #31166
BC breaks? yes
Deprecations? no
Tests pass? yes
Fixed tickets #31166
License MIT
Doc PR symfony/symfony-docs#11600

Feature request defined #31166
The purpose of this PR is to allow optional parameters in earlier segments in routes. This would allow for routes such as: /{locale}/some/route, where locale is optional to match the following:

  • /en/some/route
  • /some/route

The use case for the above example is to allow redirecting to a locale specific endpoint from the non locale specific endpoint.

This also allows for routes such as:

  • /{optional}/foo/{nonoptional}, this would match /foo/anything as well as /something/foo/anything, but not /something/foo

@Simperfit
Copy link
Contributor

Simperfit commented May 25, 2019

Coud you please check the tests as it seems broken due to this PR and we don't merge BC break it should not break BC.

@Simperfit
Copy link
Contributor

Status: Needs Work

…ers, fix route matcher dump test optional locale regex and indicies
@jrbarnard
Copy link
Author

jrbarnard commented May 25, 2019

@Simperfit , sorry I thought I'd caught the broken tests. Those should now be fixed.

This breaks BC because it's a change to how the route compiler compiles regex based on the passed route info, thus when routes are matched it matches routes that it previously wouldn't have, also for major versions is BC break not a possibility if following semver?

A way this could be added without BC break would be to refactor the RouteCompiler to be more modular and then write a new RouteCompiler that can be swapped in when wanted to allow optional parameters in other places but the end of a route?

Happy for this PR to be closed out if there is a better way to handle this / suggestions on providing optional route prefixes that would match both /en/foo/bar and /foo/bar (for example).

Cheers!

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 25, 2019

The bc break it too high risk, isn't it? Btw, why would one define a default for a currently non-optional route param? If there is no reasons for such defaults, maybe we should deprecate them, to unlock this PR for 5.1?
Alternatively, this could be enabled by an option.
(Please don't create a separate compiler, that'd create fragmentation)

@fabpot
Copy link
Member

fabpot commented May 25, 2019

This has been rejected in the past (a very long time ago). In such situations, one should have several routes.

@fabpot fabpot closed this May 25, 2019
@jrbarnard
Copy link
Author

@nicolas-grekas I agree the BC break is too high risk, unfortunately altering the compiler in anyway to accept optional parameters where they previously weren't allowed will be a BC.

There is not any reason to set a default against a non optional route parameter, however in doing so it makes the parameter optional so I don't think you could really deprecate it (as it just sets the default into the defaults array and then ignores it). Previous behaviour was that setting a default on a parameter that came before another non optional segment would just disregard it as optional and therefore compile the regex as if it was required and therefore ignore the default.

@fabpot No problem, I was trying to avoid the duplication with lots of routes but if this is the only suggested way I can stick with that, cheers

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

Successfully merging this pull request may close these issues.

5 participants