Skip to content

Commit 06bcae7

Browse files
committed
Merge branch '4.2'
* 4.2: Added Yaml, XML and PHP examples for invokable controllers
2 parents f04ac90 + c3de93a commit 06bcae7

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

controller/service.rst

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,53 @@ Controllers can also define a single action using the ``__invoke()`` method,
8484
which is a common practice when following the `ADR pattern`_
8585
(Action-Domain-Responder)::
8686

87-
// src/Controller/Hello.php
88-
use Symfony\Component\HttpFoundation\Response;
89-
use Symfony\Component\Routing\Annotation\Route;
87+
.. configuration-block::
9088

91-
/**
92-
* @Route("/hello/{name}", name="hello")
93-
*/
94-
class Hello
95-
{
96-
public function __invoke($name = 'World')
89+
.. code-block:: php-annotations
90+
91+
// src/Controller/Hello.php
92+
use Symfony\Component\HttpFoundation\Response;
93+
use Symfony\Component\Routing\Annotation\Route;
94+
95+
/**
96+
* @Route("/hello/{name}", name="hello")
97+
*/
98+
class Hello
9799
{
98-
return new Response(sprintf('Hello %s!', $name));
100+
public function __invoke($name = 'World')
101+
{
102+
return new Response(sprintf('Hello %s!', $name));
103+
}
99104
}
100-
}
105+
106+
.. code-block:: yaml
107+
108+
# app/config/routing.yml
109+
hello:
110+
path: /hello/{name}
111+
defaults: { _controller: app.hello_controller }
112+
113+
.. code-block:: xml
114+
115+
<!-- app/config/routing.xml -->
116+
<?xml version="1.0" encoding="UTF-8" ?>
117+
<routes xmlns="http://symfony.com/schema/routing"
118+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
119+
xsi:schemaLocation="http://symfony.com/schema/routing
120+
https://symfony.com/schema/routing/routing-1.0.xsd">
121+
122+
<route id="hello" path="/hello/{name}">
123+
<default key="_controller">app.hello_controller</default>
124+
</route>
125+
126+
</routes>
127+
128+
.. code-block:: php
129+
130+
// app/config/routing.php
131+
$collection->add('hello', new Route('/hello', [
132+
'_controller' => 'app.hello_controller',
133+
]));
101134
102135
Alternatives to base Controller Methods
103136
---------------------------------------

0 commit comments

Comments
 (0)