Skip to content

Commit d8e4b4d

Browse files
committed
minor #11263 Add an example to the invokable controllers section (javiereguiluz)
This PR was squashed before being merged into the 3.4 branch (closes #11263). Discussion ---------- Add an example to the invokable controllers section Fixes #7549. Invokable controllers were already explained, but we can expand the explanation a bit with an actual example. Commits ------- 6259e80 Add an example to the invokable controllers section
2 parents d856f18 + 6259e80 commit d8e4b4d

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

controller/service.rst

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,24 @@ syntax:
7878
Invokable Controllers
7979
---------------------
8080

81-
If your controller implements the ``__invoke()`` method - popular with the
82-
Action-Domain-Response (ADR) pattern, you can simply refer to the service id
83-
(``AppBundle\Controller\HelloController`` or ``app.hello_controller`` for example).
81+
Controllers can also define a single action using the ``__invoke()`` method,
82+
which is a common practice when following the `ADR pattern`_
83+
(Action-Domain-Responder)::
84+
85+
// src/AppBundle/Controller/Hello.php
86+
use Symfony\Component\HttpFoundation\Response;
87+
use Symfony\Component\Routing\Annotation\Route;
88+
89+
/**
90+
* @Route("/hello/{name}", name="hello")
91+
*/
92+
class Hello
93+
{
94+
public function __invoke($name = 'World')
95+
{
96+
return new Response(sprintf('Hello %s!', $name));
97+
}
98+
}
8499

85100
Alternatives to base Controller Methods
86101
---------------------------------------
@@ -139,3 +154,4 @@ If you want to know what type-hints to use for each service, see the
139154
.. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
140155
.. _`ControllerTrait`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
141156
.. _`AbstractController`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
157+
.. _`ADR pattern`: https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder

0 commit comments

Comments
 (0)