Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Routing] Allow query-specific parameters inUrlGenerator 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
BenMorel wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromBenMorel:_query

Conversation

BenMorel
Copy link
Contributor

@BenMorelBenMorel commentedMay 22, 2025
edited by OskarStark
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

This PR adds support for a special_query key in$parameters ofUrlGenerator::generate(), that is used exclusively to generate query parameters. This is useful when query parameters may conflict with route parameters of the same name.

Concrete use case:

My application has a route that looks like:

https://{siteCode}.{domain}/admin/stats

And I want to generate this URL:

https://fr.example.com/admin/stats?siteCode=us

With this PR, I can now call:

$urlGenerator->generate('admin_stats', ['siteCode' =>'fr','domain' =>'example.com','_query' => ['siteCode' =>'us',    ]]);

@carsonbotcarsonbot added this to the7.3 milestoneMay 22, 2025
@carsonbotcarsonbot changed the titleAllow query-specific parameters in UrlGenerator using_query[Routing] Allow query-specific parameters in UrlGenerator using_queryMay 22, 2025
@OskarStarkOskarStark changed the title[Routing] Allow query-specific parameters in UrlGenerator using_query[Routing] Allow query-specific parameters inUrlGenerator using_queryMay 22, 2025
@OskarStark
Copy link
Contributor

If someone is using_query already, it could be a BC break, which would need a deprecation first.

@BenMorel
Copy link
ContributorAuthor

If someone is using _query already, it could be a BC break, which would need a deprecation first.

I agree, unless it is considered that names starting with an underscore are reserved for Symfony, and not covered by the BC promise; I don't know about this, but there's a precedent here with_locale.

@stof
Copy link
Member

The difference is that_locale exists since the beginning of Symfony.

A better example might be to look at how we handled_fragment. I'm almost sure we added it later.

@BenMorel
Copy link
ContributorAuthor

@stof Good point._fragment was introduced in 2014, in6d79a56, PR#12979, and was first released inv3.2.0 — a minor release.

The BC break concernwas raised by@Koc back then, butwas dismissed by@Tobion:

And the tiny BC break is negligible because it's unlikely.

I'll leave it up to you to decide whether introducing another one in a minor release is still acceptable in 2025; maybe this is something that could be documented inOur Backward Compatibility Promise?

@@ -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'])) {
Copy link
Member

Choose a reason for hiding this comment

The 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_query perfectly fine. But... if I don't miss anything it must be a scalar value, right? If I am not mistaken, what about only treating_query with its special meaning for URL parameters when it is an array (at least for a transition period where we could trigger a deprecation)?

GromNaN reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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'_query' => scalar go through, although I'm not sure if this would make the whole thing even more confusing.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It's a an array in the query parameter then, no?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, it behaves just likehttp_build_query() does with arrays:?foo[x]=y (spared you the percent encoding).

@fabpotfabpot modified the milestones:7.3,7.4May 26, 2025
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Works for me, just one comment

@@ -6,6 +6,7 @@ CHANGELOG

* Allow aliases and deprecations in `#[Route]` attribute
* Add the `Requirement::MONGODB_ID` constant to validate MongoDB ObjectIDs in hexadecimal format
* Allow query-specific parameters in `UrlGenerator` using `_query`

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

BenMorel reacted with thumbs up emoji
@@ -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'])) {
throw new InvalidParameterException('The "_query" parameter must be an array.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

what about not throwing and instead fallback to the current behavior?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The 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_query:

$router->generate('...', ['_query' =>'foo']);// ?_query=foo$router->generate('...', ['_query' => ['foo' =>'bar']));//?foo=bar

And in this sense, I'd prefer to actuallyreserve_query for this purpose, and fail hard if you attempt to use it in another way.

On the other hand, doing as you suggest further diminishes the risk of breaking existing applications.

A middle ground could be the following:

  • when_query is an array: interpret it as a map of query parameters
  • when_query is a scalar:
    • in Symfony 7.4: interpret it as any other parameter, and trigger a deprecation notice
    • in Symfony 8.0: throw an exception

WDYT?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

A middle ground could be the following:

let's do this 👍

BenMorel reacted with rocket emoji
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@GromNaNGromNaNGromNaN left review comments

@OskarStarkOskarStarkOskarStark left review comments

@xabbuhxabbuhxabbuh left review comments

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

8 participants
@BenMorel@OskarStark@stof@nicolas-grekas@GromNaN@xabbuh@fabpot@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp