File tree Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -757,6 +757,43 @@ The following example shows how to detect if the user agent prefers "safe" conte
757757 $response->setContentSafe();
758758
759759 return $response;
760+
761+ Generating Relative and Absolute URLs
762+ -------------------------------------
763+
764+ .. versionadded :: 5.4
765+
766+ The feature to generate relative and absolute URLs was introduced in Symfony 5.4.
767+
768+ Generating absolute and relative URLs for a given path is a common need
769+ in some applications. In Twig templates you can use the
770+ :ref: `absolute_url() <reference-twig-function-absolute-url >` and
771+ :ref: `relative_path() <reference-twig-function-relative-path >` functions to do that.
772+
773+ The :class: `Symfony\\ Component\\ HttpFoundation\\ UrlHelper ` class provides the
774+ same functionality for PHP code via the ``getAbsoluteUrl() `` and ``getRelativePath() ``
775+ methods. You can inject this as a service anywhere in your application::
776+
777+ // src/Normalizer/UserApiNormalizer.php
778+ namespace App\Normalizer;
779+
780+ use Symfony\Component\HttpFoundation\UrlHelper;
781+
782+ class UserApiNormalizer
783+ {
784+ private UrlHelper $urlHelper;
785+
786+ public function __construct(UrlHelper $urlHelper)
787+ {
788+ $this->urlHelper = $urlHelper;
789+ }
790+
791+ public function normalize($user)
792+ {
793+ return [
794+ 'avatar' => $this->urlHelper->getAbsoluteUrl($user->avatar()->path()),
795+ ];
796+ }
760797 }
761798
762799Learn More
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ You can use this to get the value returned by the handler(s)::
1111 use Symfony\Component\Messenger\MessageBusInterface;
1212 use Symfony\Component\Messenger\Stamp\HandledStamp;
1313
14- $envelope = $messageBus->dispatch(SomeMessage());
14+ $envelope = $messageBus->dispatch(new SomeMessage());
1515
1616 // get the value that was returned by the last message handler
1717 $handledStamp = $envelope->last(HandledStamp::class);
Original file line number Diff line number Diff line change @@ -225,6 +225,8 @@ Returns the absolute URL (with scheme and host) for the given route. If
225225 Read more about :doc: `Symfony routing </routing >` and about
226226 :ref: `creating links in Twig templates <templates-link-to-pages >`.
227227
228+ .. _reference-twig-function-absolute-url :
229+
228230absolute_url
229231~~~~~~~~~~~~
230232
@@ -239,6 +241,8 @@ Returns the absolute URL (with scheme and host) from the passed relative path. C
239241:ref: `asset() function <reference-twig-function-asset >` to generate absolute URLs
240242for web assets. Read more about :ref: `Linking to CSS, JavaScript and Image Assets <templates-link-to-assets >`.
241243
244+ .. _reference-twig-function-relative-path :
245+
242246relative_path
243247~~~~~~~~~~~~~
244248
You can’t perform that action at this time.
0 commit comments