gpt4 book ai didi

symfony - 通过注入(inject)加载服务时出现问题

转载 作者:行者123 更新时间:2023-12-04 13:18:59 27 4
gpt4 key购买 nike

我正在尝试创建一个可以在任何地方注入(inject)的服务。为此,我试图将 Symfony 4.3 的 HttpClient 组件作为参数传递

我给你看服务

/image/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 ,要知道我是否创建了它,它会返回以下错误:

You have requested a non-existent service "http.client".



最后我想要实现的是这样的:
public function getClient(Back $back)
{

$back->sendCallback('GET','http://vro-back.localhost:8888/callback/test');

}

但我不能,因为我不能注入(inject)它。

最后,如果您查看 services.yml,我正在尝试为 HttpClient 组件创建别名,因此我可以将其作为参数传递给 Back 类的构造函数

我试图加载的路线存在......

Symfony \ Component \ HttpClient \ HttpClient;



这是我尝试使用的组件

https://symfony.com/doc/current/components/http_client.html

我将不胜感激任何帮助。

最佳答案

您需要键入提示 接口(interface)反而

public function __construct(HttpClientInterface $httpClient)
{
$this->httpClient = $httpClient;
}

并删除 service.yaml配置
https://symfony.com/doc/current/components/http_client.html

关于symfony - 通过注入(inject)加载服务时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56410170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com