Skip to content

Commit 0fadfa4

Browse files
committed
minor #11345 Added Yaml, XML and PHP examples for invokable controllers (TimoBakx)
This PR was merged into the 3.4 branch. Discussion ---------- Added Yaml, XML and PHP examples for invokable controllers Commits ------- e02411c Added Yaml, XML and PHP examples for invokable controllers
2 parents dc35a22 + e02411c commit 0fadfa4

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
@@ -82,20 +82,53 @@ Controllers can also define a single action using the ``__invoke()`` method,
8282
which is a common practice when following the `ADR pattern`_
8383
(Action-Domain-Responder)::
8484

85-
// src/AppBundle/Controller/Hello.php
86-
use Symfony\Component\HttpFoundation\Response;
87-
use Symfony\Component\Routing\Annotation\Route;
85+
.. configuration-block::
8886

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

0 commit comments

Comments
 (0)