[英]Symfony inject array through services.yaml
我正在尝试向事件订阅者注入一个数组,但我也注入了另一个存储库,所以我不知道这将如何工作:
这是我正在尝试的:
服务.yaml
Path\To\My\EventSubscriber\TerminateSubscriber:
arguments:
- { param1: 'myvalue', param2: 'myvalue2' }
TerminateSubscriber.php:
private Repository $repo;
/** @var array<string, string> */
private array $config;
public function __construct(Repository $repo, array $config = [])
{
$this->repo = $repo;
$this->config = $config;
}
它说第一个参数应该是给定数组的 Repo 实例。 但是,如果我切换参数,它会显示“在需要之前给出的可选参数”
编辑
数组应如下所示:
[
"a" => [400],
"b" => [400, 500],
]
您非常接近,但您需要对代码进行一些更改。 如果您需要将存储库、数组或字符串从服务传递到 class,您需要像这样传递它。
在服务中。yaml:
Path\To\My\EventSubscriber\TerminateSubscriber:
arguments: [ App\Repository\MyRepository, [MyArray],"My String Value" ]
在你的构造中:
/**
* @param Repository $repo
* @param array $config
* @param string $stringValue
*/
public function __construct(Repository $repo, array $config = [], string $stringValue)
{
$this->repo = $repo;
$this->config = $config;
$this->string = $string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.