Php 如何在Symfony服务中注入参数数组?
我正在尝试将一些细枝视图作为定制邮件服务的模板注入,这将被另一个服务用作依赖项 我不明白为什么,但好像Symfony没有看到我试图在$parameters中注入的参数 将此服务数组作为参数注入的正确方法是什么 以下是services.yaml部分:Php 如何在Symfony服务中注入参数数组?,php,symfony,symfony4,Php,Symfony,Symfony4,我正在尝试将一些细枝视图作为定制邮件服务的模板注入,这将被另一个服务用作依赖项 我不明白为什么,但好像Symfony没有看到我试图在$parameters中注入的参数 将此服务数组作为参数注入的正确方法是什么 以下是services.yaml部分: parameters: locale: 'en' template: '%fos_user.registration.confirmation.template%' resetting: '%fos_user.resetti
parameters:
locale: 'en'
template: '%fos_user.registration.confirmation.template%'
resetting: '%fos_user.resetting.email.template%'
from_email: 'somemail@mail.com'
confirmation: '%fos_user.registration.confirmation.from_email%'
resetting_password: '%fos_user.resetting.email.from_email%'
services:
user.mailer.rest:
class: App\Mailer\RestMailer
public: false
parent: fos_user.mailer.twig_swift
autoconfigure: false
autowire: true
arguments:
$parameters:
- '@template'
- '@resetting'
RestMailer服务构造函数:
public function\uu构造(\Swift\u Mailer$Mailer,urlgenerator接口$router,\Twig\u环境$Twig,数组$parameters)
{
$this->mailer=$mailer;
$this->router=$router;
$this->twig=$twig;
$this->parameters=$parameters;
}
公共函数SendConfirmationMailMessage(UserInterface$user)
{
$template=$this->parameters['template']['confirmation'];
//...
以下是返回的错误:
在DefinitionErrorExceptionPass.php第54行中:
无法自动连线服务“App\Mailer\RestMailer”:方法“\uu construct()”的参数“$parameters”>的类型提示为“array”,您应该显式配置>其值
您已经在services.yml中配置了吗
如果您想使用config.yml中的参数,您必须在services.yml中设置以使用/调用参数。将user.mailer.rest更改为App\mailer\RestMailer。Autowire正在检测您的类,并尝试创建id为App\mailer\RestMailer的第二个服务。因此出现了错误。其中一个问题。话虽如此,但是比如“@template”将注入模板服务。可能不是你想要的。所以你也需要做一些工作。