我想在我的服务类 mailer
中使用 parameters.yml
中的参数,但在实例化邮件程序类时出现此错误:
$mailer = new Mailer();
知道参数是在 parameters.yml
中定义的:
警告:缺少AppBundle \ Service \ Mailer :: __ construct()的参数1,调用
namespace AppBundle\Service
class Mailer
{
private $mailer_user;
private $mailer_password;
private $mailer_name;
private $mailer_host;
public function __construct($mailer_user, $mailer_password ,$mailer_name ,$mailer_host)
{
$this->mailer_name = $mailer_user;
$this->mailer_password = $mailer_password;
$this->mailer_user = $mailer_name;
$this->mailer_host = $mailer_host;
}
//.....
}
services.yml:
mailer:
class: 'AppBundle\Service\Mailer'
arguments: [%mailer_user%, %mailer_password% ,%mailer_name% ,%mailer_host%]
1 回答
要了解“如何在我的服务类中使用
parameters.yml
中的参数”(并了解如何在Symfony应用程序中使用该服务),请阅读Symfony docs paying attention to the Symfony version of the documentation you are reading :Introduction to Parameters
How to Set external Parameters in the Service Container
Service Container
顺便说一句,你不应该像你一样直接实现服务类:
但是从服务容器中检索服务实例(Symfony将注意自动注入所有已配置的依赖项),就像我们通常在Controller中所做的那样(下面的示例使用提供的短代码访问服务,扩展了FrameworkBundle提供的基本控制器) Symfony标准版):