Clean Way 2018
Since 2017 and Symfony 3.4 there is a much cleaner way - it's easy to configure and use.
Instead of using the anti-template of the container and service / parameter, you can pass parameters to the class through the constructor . Do not worry, this is not a time-consuming job, but setting the time and forget it.
How to configure it in 2 steps?
config.yml
# config.yml
2. Any Controller
<?php declare(strict_types=1); final class ApiController extends SymfonyController { private $apiPass; private $apiUser; public function __construct(string $apiPass, string $apiUser) { $this->apiPass = $apiPass; $this->apiUser = $apiUser; } public function registerAction(): void { var_dump($this->apiPass);
Instant upgrade is ready!
If you are using an older approach, you can automate it with the help of a rector .
Read more
This is called installing the constructor using the service localization method.
To learn more about this, check out my post. How to get the parameter in the Symfony Controller "Clean Path" .
(It has been tested and I am updating it for the new version of Symfony (5, 6 ...)).
TomΓ‘Ε‘ Votruba Mar 03 '18 at 13:02 2018-03-03 13:02
source share