Skip to content

Commit

Permalink
feat: activate and use bind examples in a controller (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
COil authored Dec 3, 2024
1 parent a0b67f3 commit cee23fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Controller/HelloWorldAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
use Symfony\Component\Routing\Attribute\Route;

/**
* We use the two binds we have defined in config/services.yaml.
*
* @see HelloWorldTest
*/
#[AsController]
#[Cache(maxage: 3600, public: true)]
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,
]);
}
}
19 changes: 19 additions & 0 deletions templates/App/Controller/HelloWorldAction.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,23 @@

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

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

<ul>
<li>environment: <b>{{ environment }}</b></li>
<li>debug: <b>{{ debug ? 'true' : 'false' }}</b></li>
</ul>

<hr/>

<p>This is just an example. You can access these two parameters thanks
to the Twig <code>app</code> special variable.</p>

<ul>
<li>app.environment: <b>{{ app.environment }}</b></li>
<li>app.debug: <b>{{ app.debug ? 'true' : 'false' }}</b></li>
</ul>

{% endblock %}

0 comments on commit cee23fa

Please sign in to comment.