File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -783,6 +783,38 @@ The following example shows how to detect if the user agent prefers "safe" conte
783783 $response->setContentSafe();
784784
785785 return $response;
786+
787+ UrlHelper
788+ -------
789+
790+ Generating absolute (and relative) URLs for a given path is a common need
791+ in lots of applications. In Twig templates this is trivial thanks to the
792+ absolute_url() and relative_path() functions. The same functionality can
793+ be found in the :class: `Symfony\\ Component\\ HttpFoundation\\ UrlHelper ` class,
794+ which can be injected as a service anywhere in your application. This class
795+ provides two public methods called getAbsoluteUrl() and getRelativePath()::
796+
797+ // src/Normalizer/UserApiNormalizer.php
798+
799+ namespace App\Normalizer;
800+
801+ use Symfony\Component\HttpFoundation\UrlHelper;
802+
803+ class UserApiNormalizer
804+ {
805+ private UrlHelper $urlHelper;
806+
807+ public function __construct(UrlHelper $urlHelper)
808+ {
809+ $this->urlHelper = $urlHelper;
810+ }
811+
812+ public function normalize($user)
813+ {
814+ return [
815+ 'avatar' => $this->urlHelper->getAbsoluteUrl($user->avatar()->path()),
816+ ];
817+ }
786818 }
787819
788820Learn More
You can’t perform that action at this time.
0 commit comments