-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Router] added appending of new optional document fragment #12979
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
[Router] added appending of new optional document fragment #12979
Conversation
8e5ec77
to
8b16335
Compare
@@ -284,6 +284,11 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa | |||
$url .= '?'.strtr($query, array('%2F' => '/')); | |||
} | |||
|
|||
// add fragment if needed | |||
if ($fragment) { | |||
$url .= '#'.rawurlencode($fragment); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are parameters a array and fragement a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it is a string, there is no natural divider that we can use to represent arrays.
👍 |
ref #3910 |
Also interface changes is a BC-break |
I didn't see that other ticket, thanks for referencing it. I would counter the reason it was closed though because...
I also didn't think of the BC break... That's unfortunate. |
I didn't realise there were special parameters (underscore prefixed) to generate, that sounds like a better approach - hopefully my reasons above will help get that other ticket reopened at least. |
8b16335
to
6fc2bd5
Compare
I reworked the patch to use a 'special' parameter inline with other Symfony special parameters for the fragment. This removes the BC issue with the interface, though would obviously cause a problem if people were already using _fragment as a valid query string parameter. |
Still a BC break as you figured yourself. |
Just a quick comment. In Drupal we have all kind of additional options, like the fragment, but also language, whether we want to render it as http or https ...
|
I agree that a helper for the fragment is useful because of the encoding need. And I cannot think of a better solution than the one provided here. And the tiny BC break is negligible because it's unlikely. Three things are missing for me to be merged:
|
1847f98
to
049f0c6
Compare
I've addressed your first two concerns. For the third I thought the best solution would be to throw a LogicException when compiling the route, as _fragment has no meaning as a path parameter. I'm not sure what you meant about "a test with a same document relative reference with just a fragment" - would that be a route with no pattern? eg. I also rebased onto current master as there were some failing lint issues. /cc @fabpot |
+1 @Tobion |
@@ -59,6 +59,13 @@ public static function compile(Route $route) | |||
$staticPrefix = $result['staticPrefix']; | |||
|
|||
$pathVariables = $result['variables']; | |||
|
|||
foreach ($pathVariables as $pathParam) { | |||
if ('_fragment' == $pathParam) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ===
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though not documented, it looks like the variables
output by compilePattern will always be strings, so a strict type equality would not be required.
Unless of course this is a coding standard, or maybe if a test could show it was required (as it seems to me without proof either way then both strict and non-strict comparison have the possibility of causing a bug).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our coding standards are that we always use strict comparison unless we explicitly need the type juggling (because type juggling can actually cause totally unexpected behavior if you are not careful).
And given we know that strings are always returned, we know we don't need this type juggling.
c005615
to
31e1d21
Compare
if ($extra && $query = http_build_query($extra, '', '&')) { | ||
// "/" and "?" can be left decoded for better user experience, see | ||
// http://tools.ietf.org/html/rfc3986#section-3.4 | ||
$url .= '?'.strtr($query, array('%2F' => '/')); | ||
} | ||
|
||
if ($fragment) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is broken for $fragment = '0'
31e1d21
to
344291d
Compare
@inso Fixed, thanks. |
@@ -59,6 +60,13 @@ public static function compile(Route $route) | |||
$staticPrefix = $result['staticPrefix']; | |||
|
|||
$pathVariables = $result['variables']; | |||
|
|||
foreach ($pathVariables as $pathParam) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array_search
?
344291d
to
5f00a59
Compare
$extra = array_diff_key($parameters, $variables, $defaults); | ||
|
||
// extract fragment | ||
$fragment = isset($extra['_fragment']) ? $extra['_fragment'] : false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default value should be ""
, not false
5f00a59
to
342d7eb
Compare
@fabpot done |
if ($extra && $query = http_build_query($extra, '', '&')) { | ||
// "/" and "?" can be left decoded for better user experience, see | ||
// http://tools.ietf.org/html/rfc3986#section-3.4 | ||
$url .= '?'.strtr($query, array('%2F' => '/')); | ||
} | ||
|
||
if (strlen($fragment)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be '' === $fragment
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'' !== $fragment
342d7eb
to
6d79a56
Compare
Thank you @rodnaph. |
…ment (rodnaph) This PR was merged into the 3.2-dev branch. Discussion ---------- [Router] added appending of new optional document fragment added a new optional parameter to the generate method for the document fragment. when specified this is appended to generated urls. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | none Commits ------- 6d79a56 [Routing] adds _fragment special option to url generation for document fragment
…ms (xabbuh) This PR was merged into the 3.2-dev branch. Discussion ---------- [Routing] treat fragment after resolving query string params | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | The implementation from #12979 led to a conflict with #18280 which was merged in the meantime. Commits ------- 7475aa8 treat fragment after resolving query string params
Why not use the more intuitive (and less conflicting) parameter key // generating a URL with a fragment (/settings#password)
$this->get('router')->generate('user_settings', ['#' => 'password']); |
@King2500 true, could be an alternative. I guess |
@King2500 I'm always in favor of short option names, but in this case I'd prefer to keep the consistency. Every special routing option in Symfony follows the same pattern: |
…(alexislefebvre) This PR was squashed before being merged into the master branch (closes #6783). Discussion ---------- Routing: add explanation for the "_fragment" parameter Add explanation about this PR: symfony/symfony#12979 Presented in http://symfony.com/blog/new-in-symfony-3-2-routing-improvements I copy-pasted the start of the announcement as the explanation since I couldn't have found a better explanation. I hope that won't be a problem. Commits ------- d657abd Routing: add explanation for the "_fragment" parameter
added a new optional parameter to the generate method for the document fragment. when specified this is appended to generated urls.