我正在尝试创建一个可以注入到任何地方的服务。为此,我试图将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:在终端中自动接线,要知道是否创建了它,它将返回以下错误:
您请求的服务“ http.client”不存在。
最后,我要实现的目标是这样的:
public function getClient(Back $back)
{
$back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');
}
但是我不能,因为我不能注射它。
最后,如果您查看services.yml,我将尝试为HttpClient组件创建别名,以便将其作为参数传递给Back类的构造函数
我尝试加载的路由存在...
Symfony \组件\ HttpClient \ HttpClient;
这是我尝试使用的组件
https://symfony.com/doc/current/components/http_client.html
我将不胜感激。
答案 0 :(得分:0)
您需要输入提示界面,而不是public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
并删除service.yaml配置 https://symfony.com/doc/current/components/http_client.html