certificationy / symfony-pack

A series of questions to prepare for the Symfony certification
https://certification.symfony.com/
MIT License
326 stars 131 forks source link

Twig path() question #35

Closed mazhuravlev closed 7 years ago

mazhuravlev commented 8 years ago

Question : True or false ? path() Twig function generates absolute urls.

According to source, RoutingExtension::path() generates not urls, but paths, and those are absolute by default, but can be made relative by setting the third parameter true. Signature of the helper is: path($name, $parameters = array(), $relative = false)

mickaelandrieu commented 8 years ago

Should be "True or false ? path() Twig function generates absolute paths ."

Good luck with the certification ! :)

mazhuravlev commented 8 years ago

Thank you!

The problem is path() can generate both relative and absolute paths. The question can be

What Twig path() function does generate?
  - absolute path (true)
  - relative path (true)
  - absolute url (false)

Also the difference between url and path is very subtle (RFC 1808 has URL BNF)

   URL         = ( absoluteURL | relativeURL )
   absoluteURL = generic-RL | ( scheme ":" *( uchar | reserved ) )
   generic-RL  = scheme ":" relativeURL
   relativeURL = net_path | abs_path | rel_path
   abs_path    = "/"  rel_path
   rel_path    = [ path ] [ ";" params ] [ "?" query ]
   path        = fsegment *( "/" segment )
   fsegment    = 1*pchar
   segment     =  *pchar
mickaelandrieu commented 8 years ago

If we look at docs, path() sounds recommended to generate relative urls/paths when url() is recommended for absolute urls.

In my opinion this is a mistake, and path() should be avoided and url() always used instead.

How about:

True or false ? url() function generates absolute urls by default.

- true (true)
- false (false)

What do you think ?

mazhuravlev commented 8 years ago

I will study if there is any need for relative urls at all in web applications and then make a pull request.

mickaelandrieu commented 7 years ago

Ok, I close the issue for now.

Don't hesitate to make a pull request if you think it's worth :)