Skip to content

Commit e753ff1

Browse files
author
Sander De la Marche
committed
Add UrlHelper section to HttpFoundation docs
1 parent 576e398 commit e753ff1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/http_foundation.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,30 @@ Session
712712

713713
The session information is in its own document: :doc:`/components/http_foundation/sessions`.
714714

715+
UrlHelper
716+
-------
717+
718+
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()::
719+
720+
Use Symfony\Component\HttpFoundation\UrlHelper;
721+
722+
class UserApiNormalizer
723+
{
724+
private UrlHelper $urlHelper;
725+
726+
public function __construct(UrlHelper $urlHelper)
727+
{
728+
$this->urlHelper = $urlHelper;
729+
}
730+
731+
public function normalize($user, $format = null, array $context = [])
732+
{
733+
return [
734+
'avatar' => $this->urlHelper->getAbsoluteUrl($user->avatar()->path()),
735+
];
736+
}
737+
}
738+
715739
Learn More
716740
----------
717741

0 commit comments

Comments
 (0)