-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Allow query-specific parameters in UrlGenerator
using _query
#60508
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,17 @@ public function generate(string $name, array $parameters = [], int $referenceTyp | |
*/ | ||
protected function doGenerate(array $variables, array $defaults, array $requirements, array $tokens, array $parameters, string $name, int $referenceType, array $hostTokens, array $requiredSchemes = []): string | ||
{ | ||
if (isset($parameters['_query'])) { | ||
if (!is_array($parameters['_query'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About the potential BC break: Right now one could use a parameter named There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea is nice, but I'm afraid arrays are accepted just fine right now. We could still lower the scope of the potential BC break by letting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's happening right now without your changes when an array is passed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a an array in the query parameter then, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it behaves just like |
||
throw new InvalidParameterException('The "_query" parameter must be an array.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about not throwing and instead fallback to the current behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like it is a bit confusing to have different behaviours for $router->generate('...', ['_query' => 'foo']); // ?_query=foo
$router->generate('...', ['_query' => ['foo' => 'bar'])); //?foo=bar And in this sense, I'd prefer to actually reserve On the other hand, doing as you suggest further diminishes the risk of breaking existing applications. A middle ground could be the following:
WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
let's do this 👍 |
||
} | ||
|
||
$queryParameters = $parameters['_query']; | ||
unset($parameters['_query']); | ||
} else { | ||
$queryParameters = []; | ||
} | ||
|
||
$variables = array_flip($variables); | ||
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters); | ||
|
||
|
@@ -260,6 +271,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem | |
|
||
// add a query string if needed | ||
$extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, fn ($a, $b) => $a == $b ? 0 : 1); | ||
$extra = array_merge($extra, $queryParameters); | ||
|
||
array_walk_recursive($extra, $caster = static function (&$v) use (&$caster) { | ||
if (\is_object($v)) { | ||
|
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.
let's move this to 7.4 now