File tree Expand file tree Collapse file tree 3 files changed +38
-1
lines changed
Expand file tree Collapse file tree 3 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -757,6 +757,39 @@ 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+ Generating absolute and relative URLs for a given path is a common need
765+ in some applications. In Twig templates you can use the
766+ :ref: `absolute_url() <reference-twig-function-absolute-url >` and
767+ :ref: `relative_path() <reference-twig-function-relative-path >` functions to do that.
768+
769+ The :class: `Symfony\\ Component\\ HttpFoundation\\ UrlHelper ` class provides the
770+ same functionality for PHP code via the ``getAbsoluteUrl() `` and ``getRelativePath() ``
771+ methods. You can inject this as a service anywhere in your application::
772+
773+ // src/Normalizer/UserApiNormalizer.php
774+ namespace App\Normalizer;
775+
776+ use Symfony\Component\HttpFoundation\UrlHelper;
777+
778+ class UserApiNormalizer
779+ {
780+ private UrlHelper $urlHelper;
781+
782+ public function __construct(UrlHelper $urlHelper)
783+ {
784+ $this->urlHelper = $urlHelper;
785+ }
786+
787+ public function normalize($user)
788+ {
789+ return [
790+ 'avatar' => $this->urlHelper->getAbsoluteUrl($user->avatar()->path()),
791+ ];
792+ }
760793 }
761794
762795Learn 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