From 832436bfc788fbb809a3d6c669dbbd9eec94a4ba Mon Sep 17 00:00:00 2001 From: Wojciech Kania Date: Sat, 20 Feb 2021 18:41:02 +0100 Subject: [PATCH] [Routing] Add PHP Attribute route example to the controller --- controller/service.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/controller/service.rst b/controller/service.rst index f8048e09def..2c592518608 100644 --- a/controller/service.rst +++ b/controller/service.rst @@ -41,6 +41,22 @@ a service like: ``App\Controller\HelloController::index``: } } + .. code-block:: php-attributes + + // src/Controller/HelloController.php + namespace App\Controller; + + use Symfony\Component\Routing\Annotation\Route; + + class HelloController + { + #[Route('/hello', name: 'hello', methods: ['GET'])] + public function index() + { + // ... + } + } + .. code-block:: yaml # config/routes.yaml @@ -105,6 +121,23 @@ which is a common practice when following the `ADR pattern`_ } } + .. code-block:: php-attributes + + // src/Controller/Hello.php + namespace App\Controller; + + use Symfony\Component\HttpFoundation\Response; + use Symfony\Component\Routing\Annotation\Route; + + #[Route('/hello/{name}', name: 'hello')] + class Hello + { + public function __invoke($name = 'World') + { + return new Response(sprintf('Hello %s!', $name)); + } + } + .. code-block:: yaml # config/routes.yaml