Symfony4.2 Autowire 捆绑服务
Symfony4.2 Autowire bundle services
我想在我自己的服务中自动装配捆绑服务接口,但出现错误:
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 包?如果是这样,也许这会对您有所帮助:
use GuzzleHttp\Client;
public function __construct()
{
$this->http_client = new Client();
}
我想在我自己的服务中自动装配捆绑服务接口,但出现错误:
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 包?如果是这样,也许这会对您有所帮助:
use GuzzleHttp\Client;
public function __construct()
{
$this->http_client = new Client();
}