Skip to content

Add some typehint on controller as service #18305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions controller/service.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ apply the ``controller.service_arguments`` tag to your controller services::
// src/Controller/HelloController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\Routing\Annotation\Route;

#[AsController]
class HelloController
{
#[Route('/hello', name: 'hello', methods: ['GET'])]
public function index()
public function index(): Response
{
// ...
}
Expand Down Expand Up @@ -71,7 +72,7 @@ a service like: ``App\Controller\HelloController::index``:
/**
* @Route("/hello", name="hello", methods={"GET"})
*/
public function index()
public function index(): Response
{
// ...
}
Expand All @@ -82,12 +83,13 @@ a service like: ``App\Controller\HelloController::index``:
// src/Controller/HelloController.php
namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class HelloController
{
#[Route('/hello', name: 'hello', methods: ['GET'])]
public function index()
public function index(): Response
{
// ...
}
Expand Down Expand Up @@ -151,7 +153,7 @@ which is a common practice when following the `ADR pattern`_
*/
class Hello
{
public function __invoke($name = 'World')
public function __invoke(string $name = 'World'): Response
{
return new Response(sprintf('Hello %s!', $name));
}
Expand All @@ -168,7 +170,7 @@ which is a common practice when following the `ADR pattern`_
#[Route('/hello/{name}', name: 'hello')]
class Hello
{
public function __invoke($name = 'World')
public function __invoke(string $name = 'World'): Response
{
return new Response(sprintf('Hello %s!', $name));
}
Expand Down Expand Up @@ -228,14 +230,14 @@ service and use it directly::

class HelloController
{
private $twig;
private Environment $twig;

public function __construct(Environment $twig)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be refactored to constructor property promotion in 6.2 after merge

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already done in 6.2

{
$this->twig = $twig;
}

public function index($name)
public function index(string $name): Response
{
$content = $this->twig->render(
'hello/index.html.twig',
Expand Down