Skip to content

Commit 07b8159

Browse files
committed
Merge branch '3.4' into 4.2
* 3.4: Add an example to the invokable controllers section
2 parents 688ecba + d8e4b4d commit 07b8159

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
@@ -80,9 +80,24 @@ a service like: ``App\Controller\HelloController::index``:
8080
Invokable Controllers
8181
---------------------
8282

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+
}
86101

87102
Alternatives to base Controller Methods
88103
---------------------------------------
@@ -141,3 +156,4 @@ If you want to know what type-hints to use for each service, see the
141156
.. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
142157
.. _`ControllerTrait`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
143158
.. _`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

Comments
 (0)