sef*_*fhi 4 service autowired symfony symfony4
我正在尝试创建一个可以注入到任何地方的服务。为此,我尝试将 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;
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我正在尝试将其加载到 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']
Run Code Online (Sandbox Code Playgroud)
问题是,如果我在终端中运行 php bin / console debug: autowiring 来了解我是否创建了它,它会返回以下错误:
您请求了一个不存在的服务“http.client”。
最后我想要实现的是这样的:
public function getClient(Back $back)
{
$back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');
}
Run Code Online (Sandbox Code Playgroud)
但我不能,因为我不能注射它。
最后,如果您查看 services.yml,我会尝试为 HttpClient 组件创建一个别名,因此我可以将其作为参数传递给 Back 类的构造函数
我试图加载的路线存在......
Symfony\Component\HttpClient\HttpClient;
这是我正在尝试使用的组件
https://symfony.com/doc/current/components/http_client.html
我将不胜感激任何帮助。
小智 6
您需要在界面上输入提示
public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}
Run Code Online (Sandbox Code Playgroud)
并删除service.yaml配置
https://symfony.com/doc/current/components/http_client.html
| 归档时间: |
|
| 查看次数: |
8909 次 |
| 最近记录: |