注入加载服务时出现问题
Problem when loading Service by injection
我正在尝试创建一个可以在任何地方注入的服务。为此,我试图将 Symfony 4.3
的 HttpClient 组件作为参数传递
我给你看服务
https://i.stack.imgur.com/2384M.png
<?php
namespace App\Service\Callback;
use Symfony\Component\HttpClient\HttpClient;
class Back
{
private $client;
public function __construct(HttpClient $httpClient)
{
$this->client = $httpClient::create();
}
public function sendCallback ( $method, $urlCallback, $option)
{
$response = $this->client->request($method,$urlCallback,$option);
$statusCode = $response->getStatusCode();
return $statusCode;
}
}
好吧,我正在尝试将其加载到 services.yml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: 'en'
base_url_front: '%env(BASE_URL_FRONT)%'
mobile_type: 2
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.
Nexy\Slack\Client: '@nexy_slack.client'
Symfony\Component\HttpClient\HttpClient: '@http.client'
# 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']
App\Service\Models\:
resource: '../src/Service/Models'
tags: ['@doctrine.orm.entity_manager','@nexy_slack.client']
slack_client:
class: 'App\Service\SlackClient'
autowire : true
arguments: ['@nexy_slack.client','@kernel']
public : true
callback_client:
class: 'App\Service\Callback\Back'
autowire: true
arguments: ['@http.client']
public: true
App\Service\Apiclient\AteneaService:
arguments: ["%kernel.environment%"]
App\Service\Apiclient\UpmService:
arguments: ["%kernel.environment%"]
App\Service\Apiclient\HermesService:
arguments: ["%kernel.environment%"]
App\Service\Socket\:
resource: '../src/Service/Socket'
tags: ['@kernel','@nexy_slack.client']
问题是,如果我 运行 php bin / console debug: autowiring in the terminal,要知道我是否创建了它,它 returns 会出现以下错误:
You have requested a non-existent service "http.client".
最后我想要达到的效果是这样的:
public function getClient(Back $back)
{
$back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');
}
但是我不能,因为我不能注射。
最后,如果您查看 services.yml,我正在尝试为 HttpClient 组件创建别名,因此我可以将其作为参数传递给 Back [=44= 的构造函数]
我正在尝试加载的路由存在...
Symfony \ Component \ HttpClient \ HttpClient;
这是我尝试使用的组件
https://symfony.com/doc/current/components/http_client.html
如有任何帮助,我将不胜感激。
您需要 type-hint 接口 而不是
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
并移除service.yaml
配置
https://symfony.com/doc/current/components/http_client.html
我正在尝试创建一个可以在任何地方注入的服务。为此,我试图将 Symfony 4.3
的 HttpClient 组件作为参数传递我给你看服务
https://i.stack.imgur.com/2384M.png
<?php
namespace App\Service\Callback;
use Symfony\Component\HttpClient\HttpClient;
class Back
{
private $client;
public function __construct(HttpClient $httpClient)
{
$this->client = $httpClient::create();
}
public function sendCallback ( $method, $urlCallback, $option)
{
$response = $this->client->request($method,$urlCallback,$option);
$statusCode = $response->getStatusCode();
return $statusCode;
}
}
好吧,我正在尝试将其加载到 services.yml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: 'en'
base_url_front: '%env(BASE_URL_FRONT)%'
mobile_type: 2
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.
Nexy\Slack\Client: '@nexy_slack.client'
Symfony\Component\HttpClient\HttpClient: '@http.client'
# 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']
App\Service\Models\:
resource: '../src/Service/Models'
tags: ['@doctrine.orm.entity_manager','@nexy_slack.client']
slack_client:
class: 'App\Service\SlackClient'
autowire : true
arguments: ['@nexy_slack.client','@kernel']
public : true
callback_client:
class: 'App\Service\Callback\Back'
autowire: true
arguments: ['@http.client']
public: true
App\Service\Apiclient\AteneaService:
arguments: ["%kernel.environment%"]
App\Service\Apiclient\UpmService:
arguments: ["%kernel.environment%"]
App\Service\Apiclient\HermesService:
arguments: ["%kernel.environment%"]
App\Service\Socket\:
resource: '../src/Service/Socket'
tags: ['@kernel','@nexy_slack.client']
问题是,如果我 运行 php bin / console debug: autowiring in the terminal,要知道我是否创建了它,它 returns 会出现以下错误:
You have requested a non-existent service "http.client".
最后我想要达到的效果是这样的:
public function getClient(Back $back)
{
$back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');
}
但是我不能,因为我不能注射。
最后,如果您查看 services.yml,我正在尝试为 HttpClient 组件创建别名,因此我可以将其作为参数传递给 Back [=44= 的构造函数]
我正在尝试加载的路由存在...
Symfony \ Component \ HttpClient \ HttpClient;
这是我尝试使用的组件
https://symfony.com/doc/current/components/http_client.html
如有任何帮助,我将不胜感激。
您需要 type-hint 接口 而不是
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
并移除service.yaml
配置
https://symfony.com/doc/current/components/http_client.html