-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] UrlHelper to get absolute URL for a path #30862
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
Conversation
@vudaltsov could you please show a small before/after example ... and maybe also briefly list the changes that existing apps may need to do. Thanks. |
@javiereguiluz , sure. final class UserApiNormalizer implements NormalizerInterface
{
private $absoluteUrlGenerator;
public function __construct(AbsoluteUrlGeneratorInterface $absoluteUrlGenerator)
{
$this->absoluteUrlGenerator = $absoluteUrlGenerator;
}
public function normalize($user, $format = null, array $context = [])
{
return [
// ...
'avatar' => $this->absoluteUrlGenerator->generateAbsoluteUrl($user->avatar()->path()),
// ...
];
}
// ...
} No changes needed for existing apps. Everything will continue to work as is. But two useful helping services will appear. |
Maybe |
naming them UrlGenerator will indeed create confusion with the routing component |
Agree, I will consider different naming today. @ro0NL , As I explained, I created contracts because you might need to decorate these services once you have some additional context for generating an absolute url. |
My idea was
is this a feature we want to sell? URL composition is a fixed concept IMHO. |
@ro0NL , I understand the idea of passing The problem is that DX is worse since almost always I will have to inject two services instead of one ( |
Refactored to |
Why is it final without an interface? Mocking this in userland will be a pain due to its dependencies and internal behavior. If it's something developers shouldn't use, I'd expect it to be |
one could pass a request stack with mocked requests, and any requestcontext. Im not sure utilities need interfaces. |
That creates a massive overhead of objects that you have to bring into the correct state in order to test your function, while their internal behavior is of no concern to your particular test if mocking would be sufficient. |
what about putting the interface in symfony/contracts? It's a bit weird for HttpFoundation (a specific concept) to introduce a polymorphic matter. |
I would not create new abstraction for that. Best would be to add that to the Request class instead. |
@fabpot, this cannot be done in |
af2971f
to
bb346b6
Compare
After discussing with @fabpot , moved to Routing, since the helper depends on the |
Btw, what's the best way to handle deprecations in |
@fabpot , looked for places in Symfony where I could replace some code with a helper call (except the Twig
But it seems that |
The latest Travis build: https://travis-ci.org/symfony/symfony/builds/516563856 |
* | ||
* @author Valentin Udaltsov <udaltsov.valentin@gmail.com> | ||
*/ | ||
final class UrlHelper |
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.
Alternative names: UrlManipulator
, UrlBuilder
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.
UrlManipulator
is nice. Let's see what other reviewers think.
UrlBuilder
is bad as a stateless service name.
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.
we need to document the deprecations in the UPDATE-4.3.md
and UPGRADE-5.0
files
df6d96c
to
3f7c70f
Compare
@xabbuh , done! |
203dce8
to
744c668
Compare
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.
minor changes to be done before merge
Ready! |
0ff8219
to
388d8f5
Compare
Thank you @vudaltsov. |
…daltsov) This PR was squashed before being merged into the 4.3-dev branch (closes #30862). Discussion ---------- [Routing] UrlHelper to get absolute URL for a path | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo I noticed that I need to generate absolute urls quite often. For example when normalizing uploads in API. I found Twig's `absolute_url()` really helpful, but obviously `Symfony\Bridge\Twig\Extension\HttpFoundationExtension` cannot be used as a normalizer's argument service. In this PR I propose to extract `HttpFoundationExtension::generateAbsoluteUrl` and `HttpFoundationExtension::generateRelativePath` to separate interfaces which could be used on their own. Although this could be just a final class helper, I thought that we might leave a possibility for decoration here. That's why I created interfaces. - [x] Split `HttpFoundationExtension` into two interfaces - [x] Deprecate `HttpFoundationExtension::generateAbsoluteUrl` and `HttpFoundationExtension::generateRelativePath` - [x] Add service definitions - [x] Fix tests - [ ] Add docs Commits ------- 388d8f5 [Routing] UrlHelper to get absolute URL for a path
Very nice feature Valentin 💥 |
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- Add UrlHelper section to HttpFoundation docs I was recently looking for a way to generate absolute URLs for assets in a service, and I finally stumbled upon the UrlHelper class after reading [this blog post](https://symfony.com/blog/new-in-symfony-4-3-url-helper), but I couldn't find any mention of it in the docs when searching for absolute_url() or anything related. I then saw [this comment on the original PR](symfony/symfony#30862 (comment)), but saw no such PR for the docs. This is my first contribution to the docs so any feedback is welcome. Commits ------- 5720f50 Add UrlHelper section to HttpFoundation docs
I noticed that I need to generate absolute urls quite often. For example when normalizing uploads in API. I found Twig's
absolute_url()
really helpful, but obviouslySymfony\Bridge\Twig\Extension\HttpFoundationExtension
cannot be used as a normalizer's argument service.In this PR I propose to extract
HttpFoundationExtension::generateAbsoluteUrl
andHttpFoundationExtension::generateRelativePath
to separate interfaces which could be used on their own. Although this could be just a final class helper, I thought that we might leave a possibility for decoration here. That's why I created interfaces.HttpFoundationExtension
into two interfacesHttpFoundationExtension::generateAbsoluteUrl
andHttpFoundationExtension::generateRelativePath