Skip to content

[Routing][Feature] add ability to mark optional parameters (when having a suffix or more than a single separator) #5424

@Tobion

Description

@Tobion

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.:

Examples:

  • /products(-by-{criterion}).html would either match /products.html with criterion = default or /products-by-price.html with criterion = '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 for product 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions