Skip to content

[HttpFoundation] UrlHelper is now aware of RequestContext changes #50309

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

Merged
merged 1 commit into from
May 19, 2023

Conversation

giosh94mhz
Copy link
Contributor

@giosh94mhz giosh94mhz commented May 12, 2023

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? yes
Tickets
License MIT
Doc PR

RequestContext in routing can change at runtime after the UrlHelper has been created, so using a RequestContextAwareInterface instance (i.e. the router) will workaround URL generation issues.

In my application I have a CLI command that dynamically create the RequestContext, based on database queries and other user-driven configuration. With this information at hand, I retrieve the router and set the new context that should then be used on any other URL generation.

This works for the Router, but then I render some twig templates (to send formatted email) and found that the UrlHelper is using the old context, and not helping at all :) Also, the helper is final and without interface, so I have no way of decorating or replacing it.

Long story short: in my opinion, the RequestContext should be treated just like the Request, and so we should use RequestContextAwareInterface just like we use the RequestStack.

....or maybe the context should be carried out with the RequestStack? Any other ideas?

no longer relevant: this PR introduce a BC on the constructor of UrlHelper, but since it's final maybe is not required to keep backward compatility. If not, I may update the class to support both RequestContextAwareInterface and RequestContext as the second parameter.

@stof
Copy link
Member

stof commented May 12, 2023

NOTE: this PR introduce a BC on the constructor of UrlHelper, but since it's final maybe is not required to keep backward compatility.

A final constructor is not an internal one. You can still write code than instantiate the class, and would be impacted by the BC break. Constructors are not only called as parent::__construct()

@giosh94mhz giosh94mhz force-pushed the urlhelper_requestcontext branch from 445807a to 2624c79 Compare May 15, 2023 12:14
@giosh94mhz
Copy link
Contributor Author

Thank you for the feedback.

A final constructor is not an internal one. You can still write code than instantiate the class, and would be impacted by the BC break. Constructors are not only called as parent::__construct()

Yes, I'm very well aware, I meant that I didn't know if you care of BC of a final helper class. Since you do, I've implemented the compatibility with the deprecation and all.

@giosh94mhz giosh94mhz force-pushed the urlhelper_requestcontext branch from 2624c79 to 59438c4 Compare May 15, 2023 12:51
@stof
Copy link
Member

stof commented May 15, 2023

https://symfony.com/bc has a good overview of the Symfony BC policy, including what kind of changes you are allowed to do as a contributor.

@carsonbot carsonbot changed the title UrlHelper is now aware of RequestContext changes [HttpFoundation] UrlHelper is now aware of RequestContext changes May 19, 2023
@nicolas-grekas nicolas-grekas force-pushed the urlhelper_requestcontext branch from 59438c4 to a016392 Compare May 19, 2023 07:21
@nicolas-grekas nicolas-grekas force-pushed the urlhelper_requestcontext branch from a016392 to 75eb469 Compare May 19, 2023 07:29
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

(I updated the patch to make it suitable for 5.4)

@nicolas-grekas
Copy link
Member

Thank you @giosh94mhz.

nicolas-grekas added a commit that referenced this pull request May 19, 2023
…changes (giosh94mhz)

This PR was merged into the 5.4 branch.

Discussion
----------

[HttpFoundation] UrlHelper is now aware of RequestContext changes

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | yes
| Tickets       |
| License       | MIT
| Doc PR        |

`RequestContext` in routing can change at runtime after the `UrlHelper` has been created, so using a `RequestContextAwareInterface` instance (i.e. the router) will workaround URL generation issues.

In my application I have a CLI command that dynamically create the RequestContext, based on database queries and other user-driven configuration. With this information at hand, I retrieve the router and set the new context that should then be used on any other URL generation.

This works for the Router, but then I render some twig templates (to send formatted email) and found that the `UrlHelper` is using the old context, and not helping at all :) Also, the helper is final and without interface, so I have no way of decorating or replacing it.

Long story short: in my opinion, the `RequestContext` should be treated just like the `Request`, and so we should use `RequestContextAwareInterface` just like we use the `RequestStack`.

....or maybe the context should be carried out with the `RequestStack`? Any other ideas?

~~no longer relevant: this PR introduce a BC on the constructor of `UrlHelper`, but since it's final maybe is not required to keep backward compatility. If not, I may update the class to support both `RequestContextAwareInterface` and `RequestContext` as the second parameter.~~

Commits
-------

a016392 UrlHelper is now aware of RequestContext changes
@nicolas-grekas nicolas-grekas merged commit d203e8e into symfony:5.4 May 19, 2023
@giosh94mhz
Copy link
Contributor Author

Thank to you guys, for all your hard work!

This was referenced May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants