Symfony4.2 Autowire捆绑服务

Symfony4.2 Autowire捆绑服务,symfony4,symfony-4.2,Symfony4,Symfony 4.2,我想在我自己的服务中自动连接捆绑包服务接口,但出现错误: Cannot resolve argument $slackOauthService of "App\Controller\Panel\Slack\SigninController::afterOauth()": Cannot autowire service "App\Service\Slack\SlackOauthService": argument "$httpClient" of method "__construct()" re

我想在我自己的服务中自动连接捆绑包服务接口,但出现错误:

Cannot resolve argument $slackOauthService of "App\Controller\Panel\Slack\SigninController::afterOauth()": Cannot autowire service "App\Service\Slack\SlackOauthService": argument "$httpClient" of method "__construct()" references interface "GuzzleHttp\ClientInterface" but no such service exists. Did you create a class that implements this interface?
我的代码是:

public function __construct( ClientInterface $httpClient, EntityManagerInterface $em ) {
        $this->httpClient = $httpClient;
        $this->em         = $em;
    }
如果我只注入实体管理器或我定义的服务,一切都可以正常工作,但无论我尝试注入什么捆绑服务(guzzle、browserkit、jms序列化程序等),它都无法工作

我为web项目定义了默认的symfony services.yaml文件:

services:
# default configuration for services in *this* file
_defaults:
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']
我使用的是Symfony4.2


这个问题的解决方案是什么?

我刚才已经讨论过了。你导入了GuzzleHttp包吗?如果是这样,这可能会帮助您:

使用GuzzleHttp\Client;
公共函数构造()
{
$this->http_client=new client();
}

如果您试图指定要包含在服务中的捆绑包,该怎么办。yaml有效吗?无效。如果我添加GuzzleHttp\:错误是:“GuzzleHttp\”的定义没有类。如果您打算在运行时动态注入此服务,请将其标记为synthetic=true。如果这是仅由子定义使用的抽象定义,请添加abstract=true,否则请指定一个类以消除此错误。如果我添加GUZLE HTTP\Client\:错误与上面的相同。这是有效的。然而,问题是为什么我不能使用DI来实现这一点?一定有什么地方有陷阱…@GeorgeOlah也许这会给你答案?确实如此。我决定使用您的解决方案,在我的应用程序名称空间上创建一个包装器服务,以便轻松替换我使用的捆绑包。如果我想为CURL使用其他任何东西,我只需要在该服务中替换它。非常感谢。