【发布时间】:2017-06-03 15:59:39
【问题描述】:
我正在使用 Symfony 并尝试创建一个服务来保存我们在 AppBundle 中的 parameters.xml 中设置的所有应用程序参数。
我上次通过将 ServiceContainer 注入服务并在其上使用 ->get('param_name') 来做到这一点,虽然我知道注入整个容器是非常糟糕的做法。
有没有一种方法可以将所有参数注入到我的服务中,而不必将它们作为 arg 添加到服务定义中??
上一个项目我实际上是这样做的
服务定义
<service id="myapp.application_parameters" class="AppBundle\DependencyInjection\Service\ApplicationParametersService">
<argument type="service" id="service_container" />
</service>
和服务等级
namespace AppBundle\DependencyInjection\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ApplicationParametersService
{
private $_container;
protected $developmentEmail;
function __construct(ContainerInterface $container)
{
$this->_container = $container;
$this->developmentEmail = $this->_container->getParameter('myapp.dev.email');
}
public function getDevEmail()
{
return $this->developmentEmail;
}
【问题讨论】:
标签: php symfony dependency-injection parameters