Skip to content

Commit 154dd7d

Browse files
add example Value Resolvers
1 parent 9e49d97 commit 154dd7d

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

controller/value_resolver.rst

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,48 @@ Symfony ships with the following value resolvers in the
8686
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestAttributeValueResolver`
8787
Attempts to find a request attribute that matches the name of the argument.
8888

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+
89106
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DateTimeValueResolver`
90107
Attempts to find a request attribute that matches the name of the argument
91108
and injects a ``DateTimeInterface`` object if type-hinted with a class
92109
extending ``DateTimeInterface``.
93110

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+
94131
By default any input that can be parsed as a date string by PHP is accepted.
95132
You can restrict how the input can be formatted with the
96133
:class:`Symfony\\Component\\HttpKernel\\Attribute\\MapDateTime` attribute.
@@ -105,20 +142,95 @@ Symfony ships with the following value resolvers in the
105142
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestValueResolver`
106143
Injects the current ``Request`` if type-hinted with ``Request`` or a class
107144
extending ``Request``.
145+
There is an example here :ref:`Request <controller-request-argument>`
108146

109147
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\ServiceValueResolver`
110148
Injects a service if type-hinted with a valid service class or interface. This
111149
works like :doc:`autowiring </service_container/autowiring>`.
112150

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+
113190
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
114191
Injects the configured session class implementing ``SessionInterface`` if
115192
type-hinted with ``SessionInterface`` or a class implementing
116193
``SessionInterface``.
117194

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+
118213
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver`
119214
Will set the default value of the argument if present and the argument
120215
is optional.
121216

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+
122234
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\UidValueResolver`
123235
Attempts to convert any UID values from a route path parameter into UID objects.
124236
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
155267
can be set to ``null`` in case the controller can be accessed by anonymous
156268
users. It requires installing the :doc:`SecurityBundle </security>`.
157269

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+
158291
If the argument is not nullable and there is no logged in user or the logged in
159292
user has a user class not matching the type-hinted class, an ``AccessDeniedException``
160293
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
166299
If the argument is not nullable and there is no logged in token, an ``HttpException``
167300
with status code 401 is thrown by the resolver to prevent access to the controller.
168301

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+
169320
:class:`Symfony\\Bridge\\Doctrine\\ArgumentResolver\\EntityValueResolver`
170321
Automatically query for an entity and pass it as an argument to your controller.
171322

0 commit comments

Comments
 (0)