-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed
Description
Currently an optional variable (that has a default and can be left out from matching and generating URLs) can only be specified without any text suffix, i.e. at the end of the pattern. This is quite limiting.
@laszlokorte and I propose to add the possibility to mark the optional parts with parentheses/curly brackets.
This can be implemented BC and would allow several new, but common use-cases, e.g.:
- one can specify optional variables anywhere in the pattern (not only at the end),
- one can use more than one char as seperator,
- an optional part that depends on multiple variables,
- flexiblity of where, what and whether there is a (trailing) seperator (see Consider adding the trailing separator as optional for optional values #5918).
Examples:
/products(-by-{criterion}).html
would either match/products.html
withcriterion = default
or/products-by-price.html
withcriterion = 'price'
/products(/{category}(.{_format}))
would be equivalent to how we already handle/products/{category}.{_format}
with defaults for both variables. It matches/products
,/products/shoes
and/products/shoes.html
. But it does not match/products.html
! Compare that to/products((/{category}).{_format})
(only brackets changed) where the format would match without category, but the category does not match without the format. So the inner part requires the outer part to match./products(-in-{category})(-by-{criterion}).html
matches/products.html
and/products-in-shoes-by-prize.html
and/products-in-shoes.html
and/products-by-prize.html
./products(-in-{category1}-and-{category2}).html
only matches the optional part when both variables match, so/products-in-shoes-and-suits.html
and/products.html
, but not/products-in-shoes.html
./products(-by-price).html
should either raise exception that there is no variable in it, or treat it as-is (with the parentheses). It must not match both/products.html
and/products-by-prize.html
(where you should usually specify 2 routes for it) because there would be no way to decide which URL to generate./products/({category})
would match/products/
and/products/...
whereas without the parentheses it would match/products
and/products/...
(see Routes of the form /hello/{name} with an optional parameter at the end don't match as expected #5869)/{product}((-by-{criterion}).{_format})
would need a default requirement forproduct
to be[^/-\.]++
(so as seperator both/
(standard) and-
and.
. Pretty hard to compile.
Also, it should raise an exception when you specify an optional part explicitly without adding a default for the variable in it.
This feature request is somewhat related to #5129 and also covers #5869 and #10217 and #9981
hason and Sydney-o9