Skip to content

Commit f7e1af2

Browse files
authored
Add some typehint
1 parent 7597ea3 commit f7e1af2

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

controller/service.rst

+9-7
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ apply the ``controller.service_arguments`` tag to your controller services::
3131
// src/Controller/HelloController.php
3232
namespace App\Controller;
3333

34+
use Symfony\Component\HttpFoundation\Response;
3435
use Symfony\Component\HttpKernel\Attribute\AsController;
3536
use Symfony\Component\Routing\Annotation\Route;
3637

3738
#[AsController]
3839
class HelloController
3940
{
4041
#[Route('/hello', name: 'hello', methods: ['GET'])]
41-
public function index()
42+
public function index(): Response
4243
{
4344
// ...
4445
}
@@ -71,7 +72,7 @@ a service like: ``App\Controller\HelloController::index``:
7172
/**
7273
* @Route("/hello", name="hello", methods={"GET"})
7374
*/
74-
public function index()
75+
public function index(): Response
7576
{
7677
// ...
7778
}
@@ -82,12 +83,13 @@ a service like: ``App\Controller\HelloController::index``:
8283
// src/Controller/HelloController.php
8384
namespace App\Controller;
8485
86+
use Symfony\Component\HttpFoundation\Response;
8587
use Symfony\Component\Routing\Annotation\Route;
8688
8789
class HelloController
8890
{
8991
#[Route('/hello', name: 'hello', methods: ['GET'])]
90-
public function index()
92+
public function index(): Response
9193
{
9294
// ...
9395
}
@@ -151,7 +153,7 @@ which is a common practice when following the `ADR pattern`_
151153
*/
152154
class Hello
153155
{
154-
public function __invoke($name = 'World')
156+
public function __invoke(string $name = 'World'): Response
155157
{
156158
return new Response(sprintf('Hello %s!', $name));
157159
}
@@ -168,7 +170,7 @@ which is a common practice when following the `ADR pattern`_
168170
#[Route('/hello/{name}', name: 'hello')]
169171
class Hello
170172
{
171-
public function __invoke($name = 'World')
173+
public function __invoke(string $name = 'World'): Response
172174
{
173175
return new Response(sprintf('Hello %s!', $name));
174176
}
@@ -228,14 +230,14 @@ service and use it directly::
228230

229231
class HelloController
230232
{
231-
private $twig;
233+
private Environment $twig;
232234

233235
public function __construct(Environment $twig)
234236
{
235237
$this->twig = $twig;
236238
}
237239

238-
public function index($name)
240+
public function index(string $name): Response
239241
{
240242
$content = $this->twig->render(
241243
'hello/index.html.twig',

0 commit comments

Comments
 (0)