[英]Dependency Injection in Symfony
我正在構建Symfony 3.3應用程序。 我在Console文件夾中有一個助手:
abstract class AbstractHelper implements HelperInterface
{
protected $httpClient;
public function __construct(HttpInterface $httpClient)
{
$this->httpClient = $httpClient;
}
}
我有執行HttpInterface命名HttpGuzzle到服務的文件夾。 我怎樣才能幫助Symfony找出我想將HttpGuzzle注入AbstractHelper構造函數中? 我試圖將這些行添加到services.yml,但是它不起作用:
AppBundle\Command\AbstractHelper:
arguments:
$httpClient: AppBundle\Service\HttpGuzzle
如果我運行測試,則會引發錯誤:
ArgumentCountError: Too few arguments to function AppBundle\Command\AbstractHelper::__construct(),
0 passed in ~/Projects/app/tests/AppBundle/Console/HelperTest.php
on line 17 and exactly 1 expected
有了這個:
helper:
class: AppBundle\Command\AbstractHelper:
arguments: [AppBundle\Service\HttpGuzzle]
我收到一個錯誤:
You have requested a non-existent service "helper".
在services.yml
您必須定義HttpGuzzle服務本身,如下所示:
httpguzzle:
class: AppBundle\Service\HttpGuzzle
然后,您可以像這樣將其傳遞給助手:
helper:
class: AppBundle\Command\AbstractHelper
arguments: ["@httpguzzle"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.