当前位置: 首页 > 知识库问答 >
问题:

使用外部存储库的symfony4问题与自动加载和参数

终彬郁
2023-03-14

我使用位于github的两个自主开发的库作为私有存储库,这样我就可以在几个项目中重用它。我包括他们通过作曲家:

"license": "proprietary",
"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/my-account/puc-web-sap-client.git",
        "options": {
            "ssl": {
                "verify_peer": "false"
            }
        }
    },
    {
        "type": "vcs",
        "url": "https://github.com/my-account/puc-web-soap-client.git",
        "options": {
            "ssl": {
                "verify_peer": "false"
            }
        }
    }
],

现在symfony抱怨找不到类和服务。我的第一个问题是:为什么它们不像symfony使用的库中的其他类那样自动加载,例如swiftmailer、phpmd、diablomedia/prettyprinter?所有都是库,默认情况下不是symfony的一部分。

然而,这里的其他回答说,我必须在我的服务中手动添加服务。yaml Symfony4将外部类库用作服务

所以我从我的库添加了大量的服务到services.yaml文件:

Puc\SapClient\:
    resource: '../vendor/puc/sap-client/src/*'
    autowire: true      # Automatically injects dependencies in your services.
    autoconfigure: true # Automatically registers your services as commands, event subscriber
    public: true


Puc\SapClient\Country\CountrySapServiceInterface:
    alias: Puc\SapClient\Country\CountrySapService

Puc\SapClient\Country\CountryService:
    autowire: true

Puc\SapClient\Currency\CurrencyService:
    autowire: true

Puc\SapClient\House\HouseService:
    autowire: true

Puc\SapClient\Ressort\RessortService:
    autowire: true

Puc\SapClient\Country\CountrySapService:
    autowire: true

Puc\SapClient\Currency\CurrencySapService:
    autowire: true
....

现在php bin/console debug:autowiring给了我以下错误:

Cannot autowire service "Puc\SapClient\Model\Currency": argument "$currencyIsoCode" of method "__construct()" is type-hinted "string", you should configure its value explicitly.

货币是一种模型,而不是服务。它由我的库填充值,并返回给我的主应用程序。这是构造函数:

public function __construct(
    string $currencyIsoCode,
    string $name,
    string $ident,
    int $numberOfDecimals
) {
    $this->currencyIsoCode = $currencyIsoCode;
    $this->name = $name;
    $this->sapIdent = $ident;
    $this->numberOfDecimals = $numberOfDecimals;
}

如何配置此模型以使用字符串?我在哪里做?我真的必须声明我的库使用的每个类吗?定义每个类的每个参数

共有1个答案

赵昊阳
2023-03-14

我认为您不必分别导入每个服务。您已经在使用“Puc\SapClient”部分执行此操作。

问题可能是您正在导入不应导入的模型。

在Symfony示例项目中,有一部分vor"services.yaml":

# 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/{Bundle,DependencyInjection,Entity,Model,Migrations,Tests,Kernel.php}'

那么你的角色就是:

# makes classes in src/ available to be used as services
  # this creates a service per class whose id is the fully-qualified class name
  Puc\SapClient\:
    resource: '../vendor/puc/sap-client/src/*'
    exclude: ''../vendor/puc/sap-client/src/{Entity,Model,"etc."}'

“等”将是服务不需要的一切。

 类似资料:
  • 我创建了一个由Maven管理的Spring Boot应用程序。我正在从我们的Maven仓库检索一个公司的图书馆。 在这个库中,我们有一个服务接口,没有用'@service'注释: 此服务只有一个实现: 这个库上下文是以旧的Spring方式(在applicationContext.xml文件中)管理的。我了解到,通常情况下,如果范围内只有一个实现,Spring Boot能够找到实现。 “我将在以下情

  • 我有一个小的外部库,它公开了许多类。 在我的symfony4项目中,我想声明我的类来自供应商,作为带有autowire和public的服务。因此,我将我的库包含在composer中,并将类似这样的psr配置添加到composer中。json: 在那之后,我试图改变我的服务。yaml进入symfony,如下所示: 如果启动测试或运行应用程序,则会返回以下错误: 如果我申报服役。yaml此界面工作正常

  • 我试图找到所有的mp3文件在存储,但我的应用程序崩溃,我使用logcat看看为什么和问题的权限访问外部存储,但我已经把权限在清单,我不知道为什么不断崩溃。第一个错误: 03-06 14:55:24.553:E/dex2OAT(2796):创建oat文件失败:/data/dalvik-cache/x86/data@app@felipe.cursoandroid.com.musicbox-2@spli

  • 我让activestorage在LocalHost上使用我的Rails5.2应用程序。 null 服务器设置如下: Nginx在计算机调用上反转代理 运行的另一台计算机上的Apache 乘客 我可以在rails编写的日志文件中看到,第二个请求的响应实际上相当快:

  • 我在使用spring存储库和服务的代码中遇到了这个问题,在我的spring项目中,它不适用于用户和角色,这是他们的服务和存储库的问题: 这是用户实现存储库: 这是用户的存储库和服务: 用户服务 试图消除空白,但这是同样的问题,我搜索了很多关于它,但没有找到任何解决方案。

  • 我有一个Xperia Neo V没有任何内部存储--只有一个SD卡作为外部存储,而Galaxy S3有内部和外部存储。 使用此函数时只能访问内部存储(S3)上的文件,但Xperia Neo V没有问题。 如何访问内部和外部存储上的文件?