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

Skip to content

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

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
Tobion opened this issue Sep 3, 2012 · 30 comments

Comments

@Tobion
Copy link
Contributor

Tobion commented Sep 3, 2012

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

@Tobion
Copy link
Contributor Author

Tobion commented Sep 3, 2012

Btw, FLOW3 has this possibility to enclose optional part: http://flow3.typo3.org/documentation/guide/partiii/routing.html#optional-route-parts

Also in ZF2 there are optional segments: http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html#zend-mvc-router-http-segment

Both solutions are quite limited and not as powerful as suggested here.

@ioleo
Copy link

ioleo commented Nov 21, 2012

👍

2 similar comments
@hason
Copy link
Contributor

hason commented Nov 22, 2012

👍

@ghost
Copy link

ghost commented Nov 23, 2012

+1

@igor-vovk
Copy link

+1

@jcrombez
Copy link
Contributor

"Milestone: 2.2" : does it means it will be in 2.2 or does it means it might be in 2.2 ?

@fabpot
Copy link
Member

fabpot commented Feb 12, 2013

it means "might be", but it won't as 2.2 is in the RC phase now.

@stof
Copy link
Member

stof commented Feb 12, 2013

I changed it to 2.3 now

@ioleo
Copy link

ioleo commented Feb 12, 2013

Hope it will be included in 2.3, this would allow really nice human-readable urls (and anyone who cares about position in google results will appreciate that!)

@Seldaek
Copy link
Member

Seldaek commented Mar 7, 2013

👍 from my part. I'm not sure what it means for ApacheDumper though. It shouldn't be a problem I suppose, this is just compiled to regexes right?

@Tobion
Copy link
Contributor Author

Tobion commented Mar 7, 2013

Yes, it's just exposing more flexibility and also compiles to regex.

@raziel057
Copy link
Contributor

+1 This feature seems usefull !

Does this PR can have a negative impact on performance?

@Tobion
Copy link
Contributor Author

Tobion commented May 5, 2013

Matching won't have a negative impact on performance. Generating URLs will be a more complex but I don't think it's a big deal.

@Koc
Copy link
Contributor

Koc commented Jul 19, 2013

Our use-case:

We are having cities, categories, categories parameters. All of this parameters is optionsl. So we have defined routes:

index: /
index_category: /{category_slug}
index_city: {city_slug}.%base_host%
index_city_category: {city_slug}.%base_host%/{category_slug}
index_city_category_parameters: {city_slug}.%base_host%/{category_slug}/{parameters_slug}
# ...

And there is madness when we need generate route:

{% set routeName = 'index' %}
{% if foo %}
  {% set routeName = 'index_foo' %}
  {% if bar %}
    {% set routeName = 'index_foo_bar' %}
  {% endif %}
{% endif %}

{{ path(routeName, {'foo': foo, 'bar': bar}) }}

@Tobion
Copy link
Contributor Author

Tobion commented Jul 19, 2013

@Koc the examples you gave only have 1 char seperator and the optional parts start at the end. I don't really see why you need multiple routes for that because that is already support at the moment. So which feature of this suggestion ticket do you need?

@Koc
Copy link
Contributor

Koc commented Jul 19, 2013

we need trailing shash everywhere and for cities we are using city_slug as subdomain.

SprosProjectBundle:Default:index:
    pattern:  /
    defaults: { _controller: SprosProjectBundle:Default:index }
    host: %base_host_metallspros%

SprosProjectBundle:Default:index_category:
    path:   /{category_slug}/
    defaults:  { _controller: SprosProjectBundle:Default:index }
    host: %host_prefix_metallspros%%base_host_metallspros%
    requirements:
        category_slug: %category_with_parameters_slug_regex%

SprosProjectBundle:Default:index_subdomain:
    pattern: /
    defaults:  { _controller: SprosProjectBundle:Default:index }
    host: "{subdomain}.%base_host_metallspros%"

SprosProjectBundle:Default:index_subdomain_category:
    path:   /{category_slug}/
    defaults:  { _controller: SprosProjectBundle:Default:index }
    host: "{subdomain}.%base_host_metallspros%"
    requirements:
        category_slug: %category_with_parameters_slug_regex%

@xmarcos
Copy link

xmarcos commented Oct 5, 2013

👍

@apfelbox
Copy link
Contributor

👍

1 similar comment
@patie
Copy link
Contributor

patie commented Jan 5, 2014

👍

@mvrhov
Copy link

mvrhov commented Feb 8, 2014

This would really be nice addition to 2.5

@zd-dalibor
Copy link

👍

2 similar comments
@mpscholten
Copy link

👍

@artem-frolov
Copy link

👍

@flokixdev
Copy link

👍

@wouterj
Copy link
Member

wouterj commented Mar 13, 2015

Any news on this PR, is it planned for 3.0? It would be very awesome!

@ste93cry
Copy link
Contributor

+1 on this issue, I would love to see it in a new Symfony release

@ioleo
Copy link

ioleo commented Mar 25, 2015

Seems like everyone would love this feature, but noone is actually working on implementing this. Am I wrong?

@Tobion
Copy link
Contributor Author

Tobion commented Mar 25, 2015

I already started it in #7051 and will try to finish it for 3.0

@ioleo
Copy link

ioleo commented Mar 25, 2015

@Tobion great, I'll have a look maybe I can help somehow 8-)

@zhil
Copy link
Contributor

zhil commented Nov 22, 2015

it would be nice to issue automate 301 redirect to "most short" url.

so, for route /products(-by-{criterion}).html (which would match /products.html with criterion = default or /products-by-price.html with criterion = 'price') when somebody request /products-by-default.html issue 301 redirect to /products.html, so no duplicate content would be generated

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