Skip to content

[Documentation][ValueResolvers] add example Value Resolvers #19427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions controller/value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <controller-request-argument>`

: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 </service_container/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.
Expand Down Expand Up @@ -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 </security>`.

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.
Expand All @@ -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.

Expand Down