From d828512d1620fdab10d67f1ef89ed6d97b9d9f2e Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 16 Jan 2024 14:38:57 +0100 Subject: [PATCH] add example Value Resolvers --- controller/value_resolver.rst | 151 ++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/controller/value_resolver.rst b/controller/value_resolver.rst index 811543efe67..dc81cf17c5a 100644 --- a/controller/value_resolver.rst +++ b/controller/value_resolver.rst @@ -86,11 +86,48 @@ Symfony ships with the following value resolvers in the :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestAttributeValueResolver` Attempts to find a request attribute that matches the name of the argument. + Example:: + + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + class DefaultController + { + #[Route('/example/{$productData}')] + public function showProduct(string $productData): Response + { + // ... + } + } + :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DateTimeValueResolver` Attempts to find a request attribute that matches the name of the argument and injects a ``DateTimeInterface`` object if type-hinted with a class extending ``DateTimeInterface``. + Example:: + + // src/Controller/DefaultController.php + // example url: http://localhost/show-date/2022-01-15T12:30:00 + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + class DefaultController + { + #[Route('/show-date/{dateParameter}')] + public function showDate( \DateTimeInterface $dateParameter): Response + { + $formattedDate = $dateParameter->format('Y-m-d H:i:s'); + + // ... + } + } + By default any input that can be parsed as a date string by PHP is accepted. You can restrict how the input can be formatted with the :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapDateTime` attribute. @@ -105,20 +142,95 @@ Symfony ships with the following value resolvers in the :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestValueResolver` Injects the current ``Request`` if type-hinted with ``Request`` or a class extending ``Request``. + There is an example here :ref:`Request ` :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\ServiceValueResolver` Injects a service if type-hinted with a valid service class or interface. This works like :doc:`autowiring `. + Example:: + + // src/Service/MyServiceInterface.php + namespace App\Service; + + interface MyServiceInterface + { + public function doSomething(): string; + } + + // src/Service/MyService.php + namespace App\Service; + + class MyService implements MyServiceInterface + { + public function doSomething(): string + { + return 'Hello from MyService!'; + } + } + + // src/Controller/DefaultController.php + namespace App\Controller; + + use App\Service\MyService; + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + class DefaultController + { + #[Route('/show-service')] + public function showService(MyService $myService): Response + { + $result = $myService->doSomething(); + + return new Response($result); + } + } + :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver` Injects the configured session class implementing ``SessionInterface`` if type-hinted with ``SessionInterface`` or a class implementing ``SessionInterface``. + Example:: + + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\HttpFoundation\Session\SessionInterface; + use Symfony\Component\Routing\Annotation\Route; + + class DefaultController + { + #[Route('/show-session')] + public function showSession(SessionInterface $session): Response + { + // ... + } + } + :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver` Will set the default value of the argument if present and the argument is optional. + Example:: + + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + class DefaultController + { + #[Route('/greet/{name}')] + public function greet(?string $name = 'Guest'): Response + { + return new Response('Hello, ' . $name . '!'); + } + } + :class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\UidValueResolver` Attempts to convert any UID values from a route path parameter into UID objects. 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 can be set to ``null`` in case the controller can be accessed by anonymous users. It requires installing the :doc:`SecurityBundle `. + Example:: + + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Component\Security\Http\Attribute\CurrentUser; + + class DefaultController + { + #[Route('/new')] + public function new( + Request $request, + #[CurrentUser] ?User $user + ): Response + { + // ... + } + } + If the argument is not nullable and there is no logged in user or the logged in user has a user class not matching the type-hinted class, an ``AccessDeniedException`` 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 If the argument is not nullable and there is no logged in token, an ``HttpException`` with status code 401 is thrown by the resolver to prevent access to the controller. + Example:: + + // src/Controller/DefaultController.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; + + class DefaultController + { + #[Route('/secured', methods: ['GET'])] + public function securedAction(TokenInterface $token): Response + { + // ... + } + } + :class:`Symfony\\Bridge\\Doctrine\\ArgumentResolver\\EntityValueResolver` Automatically query for an entity and pass it as an argument to your controller.