From 6259e80cec595e4f70df50cc8d4b511271ba1fdb Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 1 Apr 2019 16:50:48 +0200 Subject: [PATCH] Add an example to the invokable controllers section --- controller/service.rst | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/controller/service.rst b/controller/service.rst index d6c20ddc221..82df6319bac 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -78,9 +78,24 @@ syntax: Invokable Controllers --------------------- -If your controller implements the ``__invoke()`` method - popular with the -Action-Domain-Response (ADR) pattern, you can simply refer to the service id -(``AppBundle\Controller\HelloController`` or ``app.hello_controller`` for example). +Controllers can also define a single action using the ``__invoke()`` method, +which is a common practice when following the `ADR pattern`_ +(Action-Domain-Responder):: + + // src/AppBundle/Controller/Hello.php + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + /** + * @Route("/hello/{name}", name="hello") + */ + class Hello + { + public function __invoke($name = 'World') + { + return new Response(sprintf('Hello %s!', $name)); + } + } Alternatives to base Controller Methods --------------------------------------- @@ -139,3 +154,4 @@ If you want to know what type-hints to use for each service, see the .. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php .. _`ControllerTrait`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php .. _`AbstractController`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php +.. _`ADR pattern`: https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder