Php Symfony通过services.yaml注入阵列
我试图向事件订阅服务器注入一个数组,但我也注入了另一个存储库,因此我不知道这将如何工作: 以下是我正在尝试的: 服务.亚马尔Php Symfony通过services.yaml注入阵列,php,symfony,dependency-injection,Php,Symfony,Dependency Injection,我试图向事件订阅服务器注入一个数组,但我也注入了另一个存储库,因此我不知道这将如何工作: 以下是我正在尝试的: 服务.亚马尔 Path\To\My\EventSubscriber\TerminateSubscriber: arguments: - { param1: 'myvalue', param2: 'myvalue2' } TerminateSubscriber.php: private Repository $repo; /** @var array<st
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;
}
您非常接近,但需要对代码进行一些更改。如果需要将存储库、数组或字符串从服务传递到类,则需要像这样传递 在用.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;
}
您非常接近,但需要对代码进行一些更改。如果需要将存储库、数组或字符串从服务传递到类,则需要像这样传递 在用.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;
}
我如何定义数组,使其看起来像我的编辑帖子,请检查编辑我不认为您在@Thomas Britsom中会有任何问题。您可以在我拥有
[MyArray]
的服务中复制代码。尝试一下,如果有任何错误,请发回。我不知道如何在中构建该数组yaml@ThomasBritsom我如何定义数组,使其看起来像我的编辑帖子,请检查编辑我不认为您在@Thomas Britsom中会有任何问题。您可以在我拥有[MyArray]
的服务中复制代码。尝试一下,如果有任何错误,请发回。我不知道如何在中构建该数组yaml@ThomasBritsom