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

Skip to content

Add Route Annotation : explicit_defaults #28633

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
wants to merge 1 commit into from
Closed

Add Route Annotation : explicit_defaults #28633

wants to merge 1 commit into from

Conversation

chs2
Copy link
Contributor

@chs2 chs2 commented Sep 28, 2018

Allows generator to add default values to generated URL
in order to get a canonical one

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

If a route has a parameter with default value when it gets generated this parameter is not included

news_export:
  path: '/exports/news.{_format}
  requirements:
    _format: xml|json|rss
  defaults:
    _format: xml

$uri = $router -> generate('news_export')

$uri is /exports/news which is identical, from the router point of view, to /export/news.xml.

URIs should be unique to avoid duplicate content, which is bad for SEO.

Though it is possible to always add default value to parameters ($router -> generate('news_export', ['_format' => 'xml', ])), the added "explicit_defaults" ensures the route will always generated to a canonical form.

Allows generator to add default values to generated URL
in order to get a canonical one
@ro0NL
Copy link
Contributor

ro0NL commented Sep 29, 2018

what about a new path syntax to do this:

path: '/exports/news.{>_format?xml}' (note the ?xml is already possible, it's about > here).

path: '/exports/news.{{_format}}' could also do the trick.

@nicolas-grekas
Copy link
Member

I like @ro0NL's idea to deal with this at the param syntax level.
I'd suggest using an ! for that: /foo.{!bar} => this would enforce bar to have a default - and would always use it when generating URLs.
WDYT?

@nicolas-grekas nicolas-grekas added this to the next milestone Sep 29, 2018
@chs2
Copy link
Contributor Author

chs2 commented Sep 29, 2018

My original idea was to have a canonical URL generated.
Here the example has only one parameter but there could be more :

the_route:
  path: /{section}.{_format}
  requirements:
    section: (index|news|authors)
    _format: (html|xml|json)
  defaults:
    section: index
    _format: html

Having the option on the parameter level gives more flexibility but IMO defeats the purpose of the canonical URL.

(BTW, the next step I had in mind was having the Router sending a Redirect when it matches a route on a non-canonical form.)

@ro0NL
Copy link
Contributor

ro0NL commented Sep 29, 2018

we can debate if redirecting non-canonical to canonical URLs is something that belongs to core 🤔

if so, that might also be triggered by e.g. canonical: true, which IMHO is a better semantic then explicit_defaults. In that case it would create a redirect with a default value for all parameters.

@nicolas-grekas
Copy link
Member

The generated URL is already a canonical one to me. I get you'd like it to have the suffix in it instead. It's not about canonical 1/0 to me, but about suffix 1/0, isn't it?

@nicolas-grekas
Copy link
Member

@chs2 WDYT about these proposals? Would you like to implement it?

@chs2
Copy link
Contributor Author

chs2 commented Oct 21, 2018

I'd like to but I can't get my head around the param syntax especially with the XML/XSD.
I could go with "canonical: true" proposed by @ro0NL (instead of my "explicit_defaults").

@ro0NL
Copy link
Contributor

ro0NL commented Oct 22, 2018

@chs2 i dont think we need canonical:true|false, as @nicolas-grekas mentioned each configured route is/should be already a canonical one. So the options are:

  • Handle the duplicate content redirect in code / at server level (htaccess) or another router
news_export:
  path: '/exports/news.{_format}'
  defaults:
    _format: xml
  • Handle the duplicate content by another route definition
news_export_non_canonical:
  path: '/export/news'
  controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
  defaults:
    route: news_export_xml

news_export_xml:
  path: '/exports/news.xml'

These are explicit configurations to show we dont need canonical: true as a feature. The user should be responsible here.

If we look at option 2, we see our missing step: it cant specify the {_format} placeholder to be required (so it would need a route per format).

That's what we should solve by the proposed {!_format} syntax, so we can do

news_export_non_canonical:
  path: '/export/news'
  controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::redirectAction
  defaults:
    route: news_export

news_export:
  path: '/exports/news.{!_format}'
  defaults:
    _format: xml

Due ! syntax we would always require a format (given or default).

@nicolas-grekas
Copy link
Member

Thanks @chs2 for raising the point. I think the ! syntax is what we should implement.
I created #29593 to keep track of the idea.
Closing here as this is quite different.
Help wanted to implement that!

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.

4 participants