0

I'm building Symfony 3.3 application. I have a helper in a Console folder:

abstract class AbstractHelper implements HelperInterface
{
    protected $httpClient;

    public function __construct(HttpInterface $httpClient)
    {
        $this->httpClient = $httpClient;
    }
}

And I have implementation of HttpInterface named HttpGuzzle into Service folder. How could I help Symfony to figure out I want to inject HttpGuzzle into AbstractHelper constructor? I tried to add these line to services.yml but it doesn't work:

AppBundle\Command\AbstractHelper:
    arguments:
        $httpClient: AppBundle\Service\HttpGuzzle

If i run the tests it throws an error:

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

With this:

helper:
    class: AppBundle\Command\AbstractHelper:
    arguments: [AppBundle\Service\HttpGuzzle]

I get an error:

You have requested a non-existent service "helper".

1 Answer 1

1

In services.yml You have to define the HttpGuzzle service itself, like this:

httpguzzle:
    class: AppBundle\Service\HttpGuzzle

Then you can use pass it to the helper like this:

helper:
    class: AppBundle\Command\AbstractHelper
    arguments: ["@httpguzzle"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I just handled this issue. But seems it doesn't work when I trying to get service from the tests. static::createClient()->getContainer()->get('helper'); I get the same error about non-existent service.
Are you sure it's the same error? Can you post it? The fact that you're trying to instance an abstract class may have something to do with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.