-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Fixed handling of optional parameters with text suffix in the route (/foo-{bar}-{baz}.html) #5129
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
Optional parameters before static text have never been intented to be supported. So this is not a fix but a new feature request. So definitely not for 2.0. |
Well, this intention was never specified anywhere in the documentation. The documentation says that route patterns may contain variables and variables may be optional, if a default is specified. So, I would still consider not supporting optional variables before suffix a bug. What is the rationale behind explicit intention of not supporting it? |
This sounds great, i had problems regarding this issue also. But i am also not sure if it would be better to only release it for 2.1 |
as long as there is no regression involved in this, i see no reason wyh symfony should not support this feature. might be something for 2.1 though - hope its not too late there, already in beta too. |
It's definitely a BC break. |
@Tobion good point, but IMO /bar.html not matching '/bar-{foo}.html' with defaults for 'foo' was a result of a bug. I'm not sure if breaking something which was relying on erroneous behavior is a BC break. So, how should we proceed with this? If there is no way to merge it into 2.0, I can rework the PR for 2.1, but I really don't see a point in holding it until 2.2. Thoughts? |
I suggest to open a PR against master and then we can review it for 2.2. Btw, I don't understand your use case. You say you want to have a path like |
In my opinion it is really weird if /bar.html ist matched by /bar-{foo}.html because the dash(-) is not part of the optional {foo} parameter. At best /bar-.html should be matched. Otherwise /ba.html oder /b.html could be matched as well. I see that the dash(-) can be seen as some kind of separator which can be ignored if the following parameter is optional and missing but from a naiv perspective it would be really confusing. It would make more sens to mark the optional parts with parentheses like in regular expressions: Then the idea with /products-by-xxx would even make mor sense: /products(-by-{criterion}).html |
@Tobion I still consider this a bugfix, so IMO it should be included in 2.0, or 2.1 at least. Let's try to gather more opinions on that. As for my use case:
works perfectly with my fix. @laszlokorte With current Router |
I agree with @laszlokorte that there should be a syntax or an option to specify the delimiting chars for optional parameters. |
Btw, the implementation is only half-done as far as I see it. You didn't adjust the UrlGenerator, so generating such a URL with an optional parameter with text suffix is not consistent with a URL without text suffix.
generating this route without any params should result in |
Closing this PR as the current behavior is what is expected. The ending |
Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass:
Fixes the following tickets: -
Todo: -
License of the code: MIT
Documentation PR: -
The routes containing a suffix (such as fake .html extensions) following optional variables were not handled correctly. RouteCompiler was looking for the index of the first optional variable, but if the first token (starting from the end) was not a variable, but text, it was just breaking out, assuming there are no optional variables.
Fixing the detection of first optional variable lead to a problem with one of the test cases (for route '/{bar}/foo'). The RouteCompiler was creating nested optional subpatterns for optional variables, and closing them at once by calculating the difference between total number of tokens and the first optional variable's token, but it was assuming that last token (which should have closed the subpatterns) is always variable's token.
To fix that, and also a problem with correct matching of several optional variables with different requirements, I have changed it to create sequential optional subpatterns instead of nesting them. This change resulted in incorrect merging of defaults, when optional subpattern was returning an empty match (it was not possible before, because of nesting subpatterns), so I fixed mergeDefaults in UrlMatcher to skip empty matches (and effectively use defaults instead).
This fixes the following cases (which I've added to RouteCompiler's and UrlMatcher's tests):
/bar-{foo}.html (with default for foo)
/bar-{foo}-{baz}.html (with defaults for both foo and baz)
I had to adjust one of the existing RouteCompiler's tests (since the regexp has changed), but note that none of existing UrlMatcher tests were changed, meaning the fix is not breaking anything in the matching process.
I've first faced the issue while trying to create the following URL structure:
/products.html - lists first page of products, sorted by popularity
/products-by-name.html - lists first page, sorted by name
/products-2.html - the second page, by popularity
/products-by-price-2.html - the second page, by price
(more details in my Google Groups post)