且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找不到服务:即使它存在于应用程序的容器中

更新时间:2023-09-13 21:02:34

symfony4 与以前的版本相比有很多变化.您正在使用所有内容,就像在以前的版本中一样.快速解决方案不是首选"方式,但开始时可以将服务中的 public 键从默认的 false 更改为 true.yaml 文件.

A lot of things changed in symfony4 vs previous versions. You are using everything as if in a previous version. The quick solution is not the "preferred" way, but to start can change the public key from the default false to true in your services.yaml file.

更好的方法是将其保留为私有并使用依赖注入.服务的命名也发生了变化(现在只是服务的路径).请参阅此处的文档.对于您的代码,请尝试以下操作:

A better way is to leave it private and use dependency injection instead. The naming of the service has changed as well (now just the path of the service). See the docs here. For your code, try these:

// services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\Foo\Bar:
        tags: { ** }

以及具有依赖注入的控制器:

And the controller with dependence injection:

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    public function baz(Request $request, Bar $bar)
    {
        $bar->doSomething();
    }
}

有一个关于这些东西(以及更多)的很好的教程这里.

There's a nice tutorial about these things (and more) here.