@@ -80,9 +80,24 @@ a service like: ``App\Controller\HelloController::index``:
80
80
Invokable Controllers
81
81
---------------------
82
82
83
- If your controller implements the ``__invoke() `` method - popular with the
84
- Action-Domain-Response (ADR) pattern, you can refer to the service id
85
- without the method (``App\Controller\HelloController `` for example).
83
+ Controllers can also define a single action using the ``__invoke() `` method,
84
+ which is a common practice when following the `ADR pattern `_
85
+ (Action-Domain-Responder)::
86
+
87
+ // src/Controller/Hello.php
88
+ use Symfony\Component\HttpFoundation\Response;
89
+ use Symfony\Component\Routing\Annotation\Route;
90
+
91
+ /**
92
+ * @Route("/hello/{name}", name="hello")
93
+ */
94
+ class Hello
95
+ {
96
+ public function __invoke($name = 'World')
97
+ {
98
+ return new Response(sprintf('Hello %s!', $name));
99
+ }
100
+ }
86
101
87
102
Alternatives to base Controller Methods
88
103
---------------------------------------
@@ -141,3 +156,4 @@ If you want to know what type-hints to use for each service, see the
141
156
.. _`base Controller class` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
142
157
.. _`ControllerTrait` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
143
158
.. _`AbstractController` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
159
+ .. _`ADR pattern` : https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder
0 commit comments