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
757
757
$response->setContentSafe();
758
758
759
759
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
+ }
760
793
}
761
794
762
795
Learn 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)::
11
11
use Symfony\Component\Messenger\MessageBusInterface;
12
12
use Symfony\Component\Messenger\Stamp\HandledStamp;
13
13
14
- $envelope = $messageBus->dispatch(SomeMessage());
14
+ $envelope = $messageBus->dispatch(new SomeMessage());
15
15
16
16
// get the value that was returned by the last message handler
17
17
$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
225
225
Read more about :doc: `Symfony routing </routing >` and about
226
226
:ref: `creating links in Twig templates <templates-link-to-pages >`.
227
227
228
+ .. _reference-twig-function-absolute-url :
229
+
228
230
absolute_url
229
231
~~~~~~~~~~~~
230
232
@@ -239,6 +241,8 @@ Returns the absolute URL (with scheme and host) from the passed relative path. C
239
241
:ref: `asset() function <reference-twig-function-asset >` to generate absolute URLs
240
242
for web assets. Read more about :ref: `Linking to CSS, JavaScript and Image Assets <templates-link-to-assets >`.
241
243
244
+ .. _reference-twig-function-relative-path :
245
+
242
246
relative_path
243
247
~~~~~~~~~~~~~
244
248
You can’t perform that action at this time.
0 commit comments