From cee23fa71bac7ce103220c523f59ffe8515f3ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Vernet?= Date: Tue, 3 Dec 2024 18:45:35 +0100 Subject: [PATCH] feat: activate and use bind examples in a controller (#130) --- config/services.yaml | 4 ++-- src/Controller/HelloWorldAction.php | 9 +++++++-- .../App/Controller/HelloWorldAction.html.twig | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index f43615c..8b81a90 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -18,8 +18,8 @@ services: autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. bind: # bind examples - #string $environment: '%kernel.environment%' - #string $debug: '%kernel.debug%' + string $environment: '%kernel.environment%' + bool $debug: '%kernel.debug%' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name diff --git a/src/Controller/HelloWorldAction.php b/src/Controller/HelloWorldAction.php index 458e5bc..dc6a22e 100644 --- a/src/Controller/HelloWorldAction.php +++ b/src/Controller/HelloWorldAction.php @@ -11,6 +11,8 @@ use Symfony\Component\Routing\Attribute\Route; /** + * We use the two binds we have defined in config/services.yaml. + * * @see HelloWorldTest */ #[AsController] @@ -18,8 +20,11 @@ final class HelloWorldAction extends AbstractController { #[Route(path: '/hello-world', name: self::class)] - public function __invoke(): Response + public function __invoke(string $environment, bool $debug): Response { - return $this->render(self::class.'.html.twig'); + return $this->render(self::class.'.html.twig', [ + 'environment' => $environment, + 'debug' => $debug, + ]); } } diff --git a/templates/App/Controller/HelloWorldAction.html.twig b/templates/App/Controller/HelloWorldAction.html.twig index 6f94974..24125ac 100644 --- a/templates/App/Controller/HelloWorldAction.html.twig +++ b/templates/App/Controller/HelloWorldAction.html.twig @@ -7,4 +7,23 @@

This a simple hello world page without any heavy process. Just a basic Twig template rendering.

+ +

These two parameters are injected in the controller action thanks to services binds. + (@see config/services.yaml)

+ + + +
+ +

This is just an example. You can access these two parameters thanks + to the Twig app special variable.

+ + + {% endblock %}