Skip to content

[Routing] Add possibility to create a request context with parameters directly #60120

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

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Keep BC in mind
  • Loading branch information
alexander-schranz committed May 26, 2025
commit 6655b1bc60f128c103c7227fd68e1796233dffd0
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(string $baseUrl = '', string $method = 'GET', string
$this->setHttpsPort($httpsPort);
$this->setPathInfo($path);
$this->setQueryString($queryString);
$this->setParameters($parameters);
$this->setParameters([...$this->parameters, ...$parameters]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The class is not final, so we merge it with eventually parent set parameters.

Copy link
Member

Choose a reason for hiding this comment

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

which parent set parameters are you talking about ?

Copy link
Contributor Author

@alexander-schranz alexander-schranz Apr 2, 2025

Choose a reason for hiding this comment

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

A class not final like RequestContext can be extends and call inside it constructor before the parent construct the setter methods:

class MyCustomContext extends RequestContext {
     public function __construct()
     {
          $this->setParameters(['key' => 'value']);
          parent::__construct();
     }
}

See also new added test. Doing just setParameters($parameters); would override existing parameters of that class.

Copy link
Contributor Author

@alexander-schranz alexander-schranz Apr 2, 2025

Choose a reason for hiding this comment

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

we could also create a deprecation if (count($this->parameters) > 0) { and simplify this in Symfony 8?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One other question is maybe what is expected behaviour for:

$this->setParameters(['key' => 'value']);
parent::__construct(parameters: ['other' => 'test']);

If not merged we could go for bc layer with:

if (count($parameters) > 0) {
    $this->setParameters($parameters);
} elseif (count($this->parameters) > 0) {
    trigger_deprecation('symfony/routing', '7.3', 'Set parameters directly via the own constructor argument');
}

which in 8.0 gets:

$this->setParameters($parameters);

}

public static function fromUri(string $uri, string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443): self
Expand Down