@@ -86,11 +86,48 @@ Symfony ships with the following value resolvers in the
86
86
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ RequestAttributeValueResolver `
87
87
Attempts to find a request attribute that matches the name of the argument.
88
88
89
+ Example::
90
+
91
+ // src/Controller/DefaultController.php
92
+ namespace App\Controller;
93
+
94
+ use Symfony\Component\Routing\Annotation\Route;
95
+ use Symfony\Component\HttpFoundation\Response;
96
+
97
+ class DefaultController
98
+ {
99
+ #[Route('/example/{$productData}')]
100
+ public function showProduct(string $productData): Response
101
+ {
102
+ // ...
103
+ }
104
+ }
105
+
89
106
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ DateTimeValueResolver `
90
107
Attempts to find a request attribute that matches the name of the argument
91
108
and injects a ``DateTimeInterface `` object if type-hinted with a class
92
109
extending ``DateTimeInterface ``.
93
110
111
+ Example::
112
+
113
+ // src/Controller/DefaultController.php
114
+ // example url: http://localhost/show-date/2022-01-15T12:30:00
115
+ namespace App\Controller;
116
+
117
+ use Symfony\Component\Routing\Annotation\Route;
118
+ use Symfony\Component\HttpFoundation\Response;
119
+
120
+ class DefaultController
121
+ {
122
+ #[Route('/show-date/{dateParameter}')]
123
+ public function showDate( \DateTimeInterface $dateParameter): Response
124
+ {
125
+ $formattedDate = $dateParameter->format('Y-m-d H:i:s');
126
+
127
+ // ...
128
+ }
129
+ }
130
+
94
131
By default any input that can be parsed as a date string by PHP is accepted.
95
132
You can restrict how the input can be formatted with the
96
133
:class: `Symfony\\ Component\\ HttpKernel\\ Attribute\\ MapDateTime ` attribute.
@@ -105,20 +142,95 @@ Symfony ships with the following value resolvers in the
105
142
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ RequestValueResolver `
106
143
Injects the current ``Request `` if type-hinted with ``Request `` or a class
107
144
extending ``Request ``.
145
+ There is an example here :ref: `Request <controller-request-argument >`
108
146
109
147
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ ServiceValueResolver `
110
148
Injects a service if type-hinted with a valid service class or interface. This
111
149
works like :doc: `autowiring </service_container/autowiring >`.
112
150
151
+ Example::
152
+
153
+ // src/Service/MyServiceInterface.php
154
+ namespace App\Service;
155
+
156
+ interface MyServiceInterface
157
+ {
158
+ public function doSomething(): string;
159
+ }
160
+
161
+ // src/Service/MyService.php
162
+ namespace App\Service;
163
+
164
+ class MyService implements MyServiceInterface
165
+ {
166
+ public function doSomething(): string
167
+ {
168
+ return 'Hello from MyService!';
169
+ }
170
+ }
171
+
172
+ // src/Controller/DefaultController.php
173
+ namespace App\Controller;
174
+
175
+ use Symfony\Component\Routing\Annotation\Route;
176
+ use Symfony\Component\HttpFoundation\Response;
177
+ use App\Service\MyService;
178
+
179
+ class DefaultController
180
+ {
181
+ #[Route('/show-service')]
182
+ public function showService(MyService $myService): Response
183
+ {
184
+ $result = $myService->doSomething();
185
+
186
+ return new Response($result);
187
+ }
188
+ }
189
+
113
190
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ SessionValueResolver `
114
191
Injects the configured session class implementing ``SessionInterface `` if
115
192
type-hinted with ``SessionInterface `` or a class implementing
116
193
``SessionInterface ``.
117
194
195
+ Example::
196
+
197
+ // src/Controller/DefaultController.php
198
+ namespace App\Controller;
199
+
200
+ use Symfony\Component\Routing\Annotation\Route;
201
+ use Symfony\Component\HttpFoundation\Response;
202
+ use Symfony\Component\HttpFoundation\Session\SessionInterface;
203
+
204
+ class DefaultController
205
+ {
206
+ #[Route('/show-session')]
207
+ public function showSession(SessionInterface $session): Response
208
+ {
209
+ // ...
210
+ }
211
+ }
212
+
118
213
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ DefaultValueResolver `
119
214
Will set the default value of the argument if present and the argument
120
215
is optional.
121
216
217
+ Example::
218
+
219
+ // src/Controller/DefaultController.php
220
+ namespace App\Controller;
221
+
222
+ use Symfony\Component\HttpFoundation\Response;
223
+ use Symfony\Component\Routing\Annotation\Route;
224
+
225
+ class DefaultController
226
+ {
227
+ #[Route('/greet/{name}')]
228
+ public function greet(?string $name = 'Guest'): Response
229
+ {
230
+ return new Response('Hello, ' . $name . '!');
231
+ }
232
+ }
233
+
122
234
:class: `Symfony\\ Component\\ HttpKernel\\ Controller\\ ArgumentResolver\\ UidValueResolver `
123
235
Attempts to convert any UID values from a route path parameter into UID objects.
124
236
Leads to a 404 Not Found response if the value isn't a valid UID.
@@ -155,6 +267,27 @@ In addition, some components, bridges and official bundles provide other value r
155
267
can be set to ``null `` in case the controller can be accessed by anonymous
156
268
users. It requires installing the :doc: `SecurityBundle </security >`.
157
269
270
+ Example::
271
+
272
+ // src/Controller/DefaultController.php
273
+ namespace App\Controller;
274
+
275
+ use Symfony\Component\HttpFoundation\Response;
276
+ use Symfony\Component\Routing\Annotation\Route;
277
+ use Symfony\Component\Security\Http\Attribute\CurrentUser;
278
+
279
+ class DefaultController
280
+ {
281
+ #[Route('/new')]
282
+ public function new(
283
+ Request $request,
284
+ #[CurrentUser] ?User $user
285
+ ): Response
286
+ {
287
+ // ...
288
+ }
289
+ }
290
+
158
291
If the argument is not nullable and there is no logged in user or the logged in
159
292
user has a user class not matching the type-hinted class, an ``AccessDeniedException ``
160
293
is thrown by the resolver to prevent access to the controller.
@@ -166,6 +299,24 @@ In addition, some components, bridges and official bundles provide other value r
166
299
If the argument is not nullable and there is no logged in token, an ``HttpException ``
167
300
with status code 401 is thrown by the resolver to prevent access to the controller.
168
301
302
+ Example::
303
+
304
+ // src/Controller/DefaultController.php
305
+ namespace App\Controller;
306
+
307
+ use Symfony\Component\HttpFoundation\Response;
308
+ use Symfony\Component\Routing\Annotation\Route;
309
+ use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
310
+
311
+ class DefaultController
312
+ {
313
+ #[Route('/secured', methods: ['GET'])]
314
+ public function securedAction(TokenInterface $token): Response
315
+ {
316
+ // ...
317
+ }
318
+ }
319
+
169
320
:class: `Symfony\\ Bridge\\ Doctrine\\ ArgumentResolver\\ EntityValueResolver `
170
321
Automatically query for an entity and pass it as an argument to your controller.
171
322
0 commit comments