简体   繁体   English

symfony 4.1:不存在此类服务

[英]symfony 4.1: no such service exists

So I've created a new Symfony 4.1 project last week and have been working on it without any issues.所以我上周创建了一个新的 Symfony 4.1 项目,并且一直在努力工作,没有任何问题。 Until yesterday when I started using some third party dependencies.直到昨天我开始使用一些第三方依赖项。 I installed the Guzzle Client by using the composer require guzzlehttp/guzzle .我使用composer require guzzlehttp/guzzle guzzle 安装了 Guzzle 客户端。 This installed the package just fine as far as I can see.据我所知,这安装了 package 就好了。

However, when I then try to inject this service into a constructor in my code (an event or command), I get the error message: Cannot autowire service "App\xxx\Command\testCommand": argument "$client" of method "__construct()" references class "GuzzleHttp\Client" but no such service exists.但是,当我尝试将此服务注入代码(事件或命令)中的构造函数时,我收到错误消息: Cannot autowire service "App\xxx\Command\testCommand": argument "$client" of method "__construct()" references class "GuzzleHttp\Client" but no such service exists.

The Guzzle service is not the only one that doesn't work, this is just an example. Guzzle 服务不是唯一不起作用的服务,这只是一个示例。 The weird thing to me is that there are a lot of dependencies that I have that work just fine like the Doctrine DocumentManager, JMS serializer and NelmioApiDocBundle.对我来说奇怪的是,有很多依赖项都可以正常工作,例如 Doctrine DocumentManager、JMS 序列化程序和 NelmioApiDocBundle。

What I've tried so far (and actually semi-solves the problem) is adding the service name to my services.yaml file:到目前为止我尝试过的(实际上是半解决问题)是将服务名称添加到我的 services.yaml 文件中:

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.
        public: false       # Allows optimizing the container by removing unused services; this also means
                        # fetching services directly from the container via $container->get() won't work.
                        # The best practice is to be explicit about your dependencies anyway.   

    GuzzleHttp\Client:  <-- Adding this makes it work flawlessly.

The problem I have with this solution is that I would end up adding hundreds of services to my services.yaml file just to get them to work.这个解决方案的问题是我最终会在我的 services.yaml 文件中添加数百个服务,只是为了让它们工作。 It seems to me like this is not the optimal solution.在我看来,这不是最佳解决方案。

So my question is how I could resolve this in the most elegant way.所以我的问题是如何以最优雅的方式解决这个问题。 Is this something I'm doing wrong in my project?这是我在我的项目中做错了什么吗? Is it something that has to be resolved on the vendors side?这是必须在供应商方面解决的问题吗? And if so, how could it be made to work with Symfony 4.1?如果是这样,如何使它与 Symfony 4.1 一起使用?

composer require GuzzleHttp\Client

It seems like you are trying to require a class, not a package.似乎您正在尝试需要 class,而不是 package。 I was not aware composer is smart enough to recognize a package by it's provided classes, so I checked... it can't, only gives package name suggestions.我不知道作曲家足够聪明,可以通过它提供的类识别 package,所以我检查了......它不能,只给出 package 名称建议。

Make sure you understand what is a composer package.确保您了解什么是作曲家 package。

The Guzzle service is not the only one that doesn't work, this is just an example. Guzzle 服务不是唯一不起作用的服务,这只是一个示例。

You don't have a Guzzle service.您没有 Guzzle 服务。 You only have the guzzle library with it's classes.你只有 guzzle 库和它的类。 Services require a service definition, like the one you wrote.服务需要一个服务定义,就像你写的那样。

The other services like Doctrine you mentioned are working, because they are bundles, which provide service definitions.您提到的 Doctrine 等其他服务正在运行,因为它们是提供服务定义的捆绑包。

Either you use a bundle, or you use a library, and hook it up yourself.要么使用捆绑软件,要么使用库,然后自己连接。

In the case of guzzle, I would not use a bundle.在 guzzle 的情况下,我不会使用捆绑包。 Then again, I would not use guzzle directly anymore, but HTTPlug.再说一次,我不会再直接使用 guzzle,而是使用 HTTPlug。 (bundle: https://github.com/php-http/HttplugBundle , library: https://github.com/php-http/httplug ) (捆绑: https://github.com/php-http/HttplugBundle ,库: https://github.com/php-http/httplug

In my case, I just needed to re-generate the optimized autoload files.就我而言,我只需要重新生成优化的自动加载文件。

So using composer dump-autoload solved the issue for me.所以使用composer dump-autoload为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM