Skip to content

Add UrlHelper section to HttpFoundation docs #16176

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
Dec 3, 2021

Conversation

sanderdlm
Copy link

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, 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, but saw no such PR for the docs.

This is my first contribution to the docs so any feedback is welcome.

@carsonbot carsonbot added this to the 5.4 milestone Nov 29, 2021
@gnito-org
Copy link
Contributor

Isn't the more accepted way to generate absolute URLs by using Symfony\Component\Routing\Generator\UrlGeneratorInterface::generate and passing Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL as the third argument?

@sanderdlm
Copy link
Author

As far as I know, UrlGeneratorInterface is only used for routes, whileUrlHelper allows you to make any link absolute, provided that you're within a request context in your application (it uses requestStack and requestContext). That explains why the class is under the HttpFoundation component and not Routing.

The use-case is different too. I use it to generate absolute images before I send them to Slack using the Symfony Notifier for example. Combined with the Packages service from the Asset component, you can really easily generate an absolute URL for your image, just like you would with {{ absolute_url(asset(imagePath)) }} in Twig

Copy link
Contributor

@gnito-org gnito-org left a comment

Choose a reason for hiding this comment

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

In your code example, please:

  • Change Use to use,
  • At the top, add a file and namespace (// src/Normalizer/UserApiNormalizer.php and namespace App\Normalizer;).

Copy link
Contributor

@gnito-org gnito-org left a comment

Choose a reason for hiding this comment

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

Thank you @dreadnip

$this->urlHelper = $urlHelper;
}

public function normalize($user, $format = null, array $context = [])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public function normalize($user, $format = null, array $context = [])
public function normalize($user): array

as seems not used :)
do not know if we can typehint the $user tho

Copy link
Author

Choose a reason for hiding this comment

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

UrlHelper
-------

Generating absolute (and relative) URLs for a given path is a common need in lots of applications. In Twig templates this is trivial thanks to the absolute_url() and relative_path() functions. The same functionality can be found in the :class:`Symfony\\Component\\HttpFoundation\\UrlHelper` class, which can be injected as a service anywhere in your application. This class provides two public methods called getAbsoluteUrl() and getRelativePath()::
Copy link
Contributor

Choose a reason for hiding this comment

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

on sf doc, it is tried to keep lines to ~ 70/80 chars then go to new line :)

Copy link
Author

Choose a reason for hiding this comment

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

@javiereguiluz javiereguiluz merged commit f25896d into symfony:5.4 Dec 3, 2021
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.

5 participants