Skip to content

Commit 9a37b34

Browse files
committed
minor #15008 [Routing] Add PHP Attribute route example to the controller (wkania)
This PR was merged into the 5.2 branch. Discussion ---------- [Routing] Add PHP Attribute route example to the controller <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `5.x` for features of unreleased versions). --> Commits ------- 832436b [Routing] Add PHP Attribute route example to the controller
2 parents fbd07a8 + 832436b commit 9a37b34

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

controller/service.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ a service like: ``App\Controller\HelloController::index``:
4141
}
4242
}
4343
44+
.. code-block:: php-attributes
45+
46+
// src/Controller/HelloController.php
47+
namespace App\Controller;
48+
49+
use Symfony\Component\Routing\Annotation\Route;
50+
51+
class HelloController
52+
{
53+
#[Route('/hello', name: 'hello', methods: ['GET'])]
54+
public function index()
55+
{
56+
// ...
57+
}
58+
}
59+
4460
.. code-block:: yaml
4561
4662
# config/routes.yaml
@@ -105,6 +121,23 @@ which is a common practice when following the `ADR pattern`_
105121
}
106122
}
107123
124+
.. code-block:: php-attributes
125+
126+
// src/Controller/Hello.php
127+
namespace App\Controller;
128+
129+
use Symfony\Component\HttpFoundation\Response;
130+
use Symfony\Component\Routing\Annotation\Route;
131+
132+
#[Route('/hello/{name}', name: 'hello')]
133+
class Hello
134+
{
135+
public function __invoke($name = 'World')
136+
{
137+
return new Response(sprintf('Hello %s!', $name));
138+
}
139+
}
140+
108141
.. code-block:: yaml
109142
110143
# config/routes.yaml

0 commit comments

Comments
 (0)