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
757
757
$response->setContentSafe();
758
758
759
759
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
+ }
760
797
}
761
798
762
799
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